Day21.Python project case

Project Practice###

1、 Batch modify file name
2、 Sending emails --- Check in and receive can be configured.
3、 Batch synthesis of avatar images
4、 Development of stock inquiry program --- check in and receive
5、 Lottery program (source code dedication)

0、 An artificial intelligence dialogue code worth 200 million###

 # ! /usr/bin/env python
 # - *- coding:utf-8-*-while True:print(input().strip("Is it??")+"!")

1、 Python batch modify file name###

 #! /usr/bin/env python
 # - *- coding:utf-8-*-
 __ author__ ='JackFeng'
 
 # Batch modify file name
 # Modify image file names in batch
 # Features
  # Take the example of batch editing all picture names in a folder
  # The comments are super detailed, and the universal template, readers can draw inferences from one another, modify the template appropriately, and the effect is remarkable!
 
 import  os
 import sys
 import re
 
 def renameall():
  # filelist = os.listdir(r"File path") #To be modified folder
  filelist = os.listdir(r"C:\\Users\\66860\\Desktop\\Test file")print("before fixing"+str(filelist)) #Files contained in the output folder
  currentpath = os.getcwd() #Get the current working directory of the process
  os.chdir(r"C:\\Users\\66860\\Desktop\\Test file")  #Modify the current working directory to the location of the folder to be modified
 
  a=input("Please enter the name you want to change:")
  num =1  #Name variable
  print(a)for fileName in filelist: #Traverse all files in the folder
   pat=".+\.(pdf|PDF)" #Regular expression for matching file names#Here is a modified pdf case,So use pdf format
   pattern = re.findall(pat,fileName) #Make a match
   # print(pattern)
   os.rename(fileName,(str(a)+str(num)+'.'+pattern[0])) #File rename
   num +=1 #Change the number and continue to the next item
   # os.rename(fileName,(str(a)+'.'+ pattern[0]))print("=========---wind-Order-limit---===========")
  os.chdir(currentpath) #Change back to the working directory before the program was run
  sys.stdin.flush() #Refresh
  print("After modification:"+str(os.listdir(r"C:\\Users\\66860\\Desktop\\Test file"))) #Output the files contained in the modified folder
  print("==========--Modification completed--===========")renameall()

3、 Batch synthesis of avatars###

 #! /usr/bin/env python
 # - *- coding:utf-8-*-
 __ author__ ='JackFeng'
 
 # @ Time    :2020/5/1810:14
 # @ Author  : JackFeng
 # @ FileName: Photo.py
 # @ Software: PyCharm
 # @ Blog    :http://www.a2data.cn/import PIL.Image as Image
 import os
 
 
 # Define the location of the initial picture
 x =0
 y =0
 
 # Get the downloaded avatar file
 curr_dir ="D://"
 ls = os.listdir(curr_dir +'1')print(ls)
 
 # Set friend's WeChat wall size Image.new()method.
 image = Image.new("RGB",(600,600))
 
 # Open WeChat friend picture Image.open()method.
 # img = Image.open(r"D:/1/1.png")
 
 # Reset WeChat avatar to small image using img.resize()method
 # img = img.resize((50,50), Image.ANTIALIAS)
 
 # Paste the photo into our WeChat wall using image.paste()method.
 # image.paste(img,(x *50, y *50))
 
 # Finally, save the completed photo wall and use image.save()method.
 # img = image.save("D:\\1\\WeChat_Friends.jpg")
 
 
 
 # Iterate through the pictures of a folder
 for file_names in ls:print(file_names)try:
   # Open pictures one by one
   img = Image.open(curr_dir +"1/"+ file_names)print(img)
  except IOError:continueelse:
   # Resize the picture
   img = img.resize((150,150), Image.ANTIALIAS)
   # Paste the picture on the final photo wall
   image.paste(img,(x *100, y *150))
   # Set 13 images per row
   x +=1if x ==6:
    x =0
    y +=1
 # Save picture as WeChat_Friends.jpg
 img = image.save(curr_dir +"WeChat_Friends.jpg")

4、 Books in the lottery###

 #! /usr/bin/env python # -*- coding:utf-8-*-__author__ ='Jack'
# @ Time    :2019/8/2211:07# @Author  : JackFeng# @Software: PyCharm# @Blog    :http://www.a2data.cn/classLuckgame:
  # Import the popup module
  import tkinter.messagebox as mbox
  # Import tkinter
  import tkinter as tk
  # Import thread module
  import threading
  import time
  def __init__(self):
   self.window = self.tk.Tk()
   self.window.title('DataScience-Lucky draw')
   self.window.minsize(330,330)
 
   # Set initial variables
   # Set interface
   self.set_label_btn()
   # setting menu
   self.set_menu()
   # List of prizes
   self.gifts =[self.lab1, self.lab2, self.lab3, self.lab4, self.lab5, self.lab6, self.lab7, self.lab8, self.lab9,
      self.lab10, self.lab11, self.lab12]
   # # Whether to open the loop flag
   # self.isloop = False
   # Whether to terminate the turntable
   self.run_flag = False
 
   self.window.mainloop()
 
  # setting menu
  def set_menu(self):
   # Create total menu
   menubar = self.tk.Menu(self.window)
   # Create a drop-down menu and add the file menu
   filemenu = self.tk.Menu(menubar, tearoff=0)
   # Options to create a menu
   filemenu.add_command(label="exit the game", command=self.window.quit)
 
   # print author function
   def show_author():
    self.mbox.showinfo(title='WeChat public platform: DataScience', message='DataScience:Ai_Craziest')
 
   filemenu.add_command(label="Author", command=show_author)
   # Add the file menu as a drop-down menu to the general menu and name it Action
   menubar.add_cascade(label="operating", menu=filemenu)
   # Show general menu
   self.window.config(menu=menubar)
 
  def set_label_btn(self):
   self.lab1 = self.tk.Label(self.window, text='in principle', bg='red')
   self.lab1.place(x=20, y=20, width=60, height=60)
 
   self.lab2 = self.tk.Label(self.window, text='Hive\n Programming Guide', bg='white')
   self.lab2.place(x=90, y=20, width=60, height=60)
 
   self.lab3 = self.tk.Label(self.window, text='IDEA\nOperation Manual', bg='white')
   self.lab3.place(x=160, y=20, width=60, height=60)
 
   self.lab4 = self.tk.Label(self.window, text='MySql\n must know and know', bg='white')
   self.lab4.place(x=230, y=20, width=60, height=60)
 
   self.lab5 = self.tk.Label(self.window, text='SQL\nCook', bg='white')
   self.lab5.place(x=230, y=90, width=60, height=60)
 
   self.lab6 = self.tk.Label(self.window, text='Python\nCook', bg='white')
   self.lab6.place(x=230, y=160, width=60, height=60)
 
   self.lab7 = self.tk.Label(self.window, text='Python\n machine learning', bg='white')
   self.lab7.place(x=230, y=230, width=60, height=60)
 
   self.lab8 = self.tk.Label(self.window, text='Python\n machine training', bg='white')
   self.lab8.place(x=160, y=230, width=60, height=60)
 
   self.lab9 = self.tk.Label(self.window, text='Big Data\n interview questions', bg='white')
   self.lab9.place(x=90, y=230, width=60, height=60)
 
   self.lab10 = self.tk.Label(self.window, text='Machine learning\n personal notes', bg='white')
   self.lab10.place(x=20, y=230, width=60, height=60)
 
   self.lab11 = self.tk.Label(self.window, text='Data mining\nConcept and technology', bg='white')
   self.lab11.place(x=20, y=160, width=60, height=60)
 
   self.lab12 = self.tk.Label(self.window, text='Handbook\n(Original)', bg='white')
   self.lab12.place(x=20, y=90, width=60, height=60)
 
   # Start button
   self.btn_go = self.tk.Button(self.window, text='Start', command=self.start_stak)
   self.btn_go.place(x=90, y=125, width=50, height=50)
   # stop button
   self.btn_stop = self.tk.Button(self.window, text='End', command=self.stop_run)
   self.btn_stop.place(x=160, y=125, width=50, height=50)
 
  def run_gifts(self):
   # Variable i for while loop count
   i =0
   # Loop all labels,Change the color of the label in turn,Turn it red
   while True:if self.run_flag:
     self.mbox.showinfo(title='Thank you for your attention, kind reminder', message='DataScience: Congratulations on getting:'+ self.gifts[i -1]['text']+'E-book')
     self.mbox.showinfo(title='If it helps you, please like and share', message='Please go to WeChat public account: DataScience to receive your prizes! ! !')
     self.mbox.showinfo(title='If you have more needs, please share this article to Moments', message='Keep the screenshots for more than three hours and send them to the DataScience backend, then you can get any three copies! !')
     self.mbox.showinfo(title='The first small interaction of this official account, thank you for your participation', message='Thank you for your participation, the next issue will be more exciting with you! !')
     # Whether to reset the parameters of the cyclic turntable
     self.run_flag = False
     returntry:
     # Add a delay operation
     self.time.sleep(0.1)
     # Loop all labels,Change the color of all labels to white
     for v in self.gifts:
      v['bg']='white'
     self.gifts[i]['bg']='red'
     i +=1
     # The label component count is reset to zero
     if i >=len(self.gifts):
      i =0
    except:
     pass
 
  def stop_run(self):
   self.run_flag = True
 
  # Start the thread and run a loop function separately
  def start_stak(self):
   # New thread
   tread = self.threading.Thread(target=self.run_gifts)
   # Start thread
   tread.start()
   # # Flag to turn on looping threads
   # self.isloop = True
 
 
 # Call Luckgame game object
 nsf =Luckgame()

5、 Rolling lottery

 #! /usr/bin/env python
 # - *- coding:utf-8-*-
 __ author__ ='Jack'
 
 # @ Time    :2020/6/2211:07
 # @ Author  : JackFeng
 # @ FileName: Lotto.py
 # @ Software: PyCharm
 # @ Blog    :http://www.a2data.cn/import sys
 if sys.version_info[0]==2:import Tkinter
  from Tkinter import*else:import tkinter as Tkinter
  from tkinter import*import random
 
 data =['Kath','Big maple','Jack','Shon','DuRuo']
 going = True
 is_run = False
 
 
 def lottery_roll(var1, var2):
  global going
  show_member = random.choice(data)
  var1.set(show_member)if going:
   window.after(50, lottery_roll, var1, var2)else:
   var2.set('Congratulations{} !!!'.format(show_member))
   going = True
   return
 
 
 def lottery_start(var1, var2):
  global is_run
  if is_run:return
  is_run = True
  var2.set('Is it you lucky? . .')lottery_roll(var1, var2)
 
 
 def lottery_end():
  global going, is_run
  if is_run:
   going = False
   is_run = False
 
 
 if __name__ =='__main__':
  window = Tkinter.Tk()
  window.geometry('405x320+250+15')
  window.title('DataScience Rolling Lottery')
 
  bg_label =Label(window, width=70, height=24, bg='#ECf5FF')
  bg_label.place(anchor=NW, x=0, y=0)
 
  var1 =StringVar(value='About to start')
  show_label1 =Label(window, textvariable=var1, justify='left', anchor=CENTER, width=17, height=3, bg='#BFEFFF',
       font='Italics-40 bold', foreground='black')
  show_label1.place(anchor=NW, x=21, y=20)
  var2 =StringVar(value='Is it you lucky? . .')
  show_label2 =Label(window, textvariable=var2, justify='left', anchor=CENTER, width=38, height=3, bg='#ECf5FF',
       font='Italics-18 bold', foreground='red')
  show_label2.place(anchor=NW, x=21, y=240)
 
  button1 =Button(window, text='Start', command=lambda:lottery_start(var1, var2), width=14, height=2, bg='#A8A8A8',
      font='Song Ti-18 bold')
  button1.place(anchor=NW, x=20, y=175)
  button2 =Button(window, text='End', command=lambda:lottery_end(), width=14, height=2, bg='#A8A8A8',
      font='Song Ti-18 bold')
  button2.place(anchor=NW, x=232, y=175)
 
  window.mainloop()

Recommended Posts

Day21.Python project case
Day18.python file/directory