Detailed Explanation of Main Function Design of Python Enterprise Code Generation System

  • 2021-07-26 08:37:09
  • OfStack

This paper describes the main function design of Python enterprise code generation system with examples. Share it for your reference, as follows:

1 Description of main functions

函数 功能
mkdir 判断保存防伪码或补充防伪码的文件夹是否存在,如果不存在则建立文件夹。
openfile 读取文本文件函数,主要读取保存产品编码和生成数量的文件mrsoft.mri,以及用户选择的已生成的编码文件。
inputbox 输入验证判断函数,根据参数判断输入的是哪种类型,是否合法
wfile 编码输出显示函数,通过屏幕输出和文件输出两种方式输出生成的防伪码信息。

2 mkdir function implementation


#  Create folder function 
def mkdir(path):
  isexists = os.path.exists(path) #  Determine whether the folder path exists 
  if not isexists: #  If the folder path does not exist 
    os.mkdir(path) #  Create the folder you want to create 

3 openfile function implementation


#  Function for reading file contents 
def openfile(filename):
  f = open(filename) #  Open the specified file 
  fllist = f.read() #  Read the contents of a file 
  f.close() #  Close a file 
  return fllist #  Returns the contents of the read file 

4 inputbox function implementation


# 输入验证函数,showstr为input函数提供动态输入提示文字,showorder提供验证方式,length提供要求输入数据的长度
def inputbox(showstr, showorder, length):
  instr = input(showstr) # 使用input函数要求用户输入信息,showstr为输入提示文字
  if len(instr) != 0: # 输入数据的长度不为零
    # 根据输入数据的要求,分成3种验证方式验证,1:数字,不限位数;2:字母;3:数字且有位数要求
    if showorder == 1: # 验证方式 ,数字格式,不限位数,大于零的整数
      if str.isdigit(instr): # 验证是否为数字
        if instr == 0: # 验证数字是否为0,如果是,要求重新输入,返回值为0
          print("\033[1;31;40m 输入为零,请重新输入!!\033[0m") # 要求重新输入,返回值为“0”
          return "0" # 函数返回值为“0”,为什么返回值为“0”呢?读者思考1下
        else: # 如果输入正确,返回输入的数据给返回值
          return instr # 将输入的数据传给函数返回值
      else: # 如果输入不是数字,要求用户重新输入,函数返回值为“0”
        print("\033[1;31;40m输入非法,请重新输入!!\033[0m") # 要求用户重新输入
        return "0" # 函数返回值为“0”
    if showorder == 2: # 验证方式2 ,要求字母格式,且是3个字母
      if str.isalpha(instr): # 判断输入是否为字母
        if len(instr) != length: # 判断输入的是否为3个字母,如果不是,则要求重新输入,返回值为“0”
          print("\033[1;31;40m必须输入3个字母,请重新输入!!\033[0m") # 要求重新输入
          return "0" # 返回值为“0”
        else: # 如果输入是3个字母,返回输入的字母
          return instr # 将函数返回值设置为输入的字母
      else: # 如果输入不是字母
        print("\033[1;31;40m输入非法,请重新输入!!\033[0m") # 要求重新输入
        return "0" # 返回值为“0”
    if showorder == 3: # 验证方式3 ,要求数字格式,且输入数字位数有要求
      if str.isdigit(instr): # 验证是否为数字
        if len(instr) != length: # 验证输入数字是否为要求长度位数,如果不是3位数字,则要求重新输入
          print("\033[1;31;40m必须输入" + str(length) + "个数字,请重新输入!!\033[0m") # 要求重新输入
          return "0" # 返回值为“0”
        else: # 输入数字满足要求,设置函数返回值为输入信息
          return instr # 设置函数返回值为输入信息
      else: # 如果输入不是数字
        print("\033[1;31;40m输入非法,请重新输入!!\033[0m") # 提示输入非法,要求重新输入
        return "0" # 函数返回值为“0”
  else: # 如果没有输入任何内容,即输入为空
    print("\033[1;31;40m输入为空,请重新输入!!\033[0m") # 提示输入为空,要求重新输入
    return "0" # 函数返回值为“0”

5 wfile function implementation


#  Realize screen output and file output coding information function, # sstr Parameter is output anti-counterfeiting code data , sfile Is the file name of the output 
# typeis Set whether to prompt through the message box after the output is completed , smsg Is the prompt text of the information prompt box, datapath  Folder for saving security codes 
def wfile(sstr, sfile, typeis, smsg, datapath):
  mkdir(datapath) #  Call this function to create a folder 
  datafile = datapath + "\\" + sfile #  Set the file to save the security code (including the path) 
  file = open(datafile, 'w') #  Open the file where the security code is saved, and create the file if it does not exist 
  wrlist = sstr #  Assign the security code information to wrlist
  pdata = "" #  Empty variable pdata , pdata Store the security code information output by the screen 
  wdata = "" #  Empty variable  wdata  ,  wdata  Storing security code information saved to a text file 
  for i in range(len(wrlist)): #  Read anti-counterfeiting code data by strip cycle 
    wdata = str(wrlist[i].replace('[', '')).replace(']', '') #  Remove brackets from characters 
    wdata = wdata.replace(''''','').replace(''''', '') #  Remove quotation marks from characters 
    file.write(str(wdata)) #  Write the file where the security code is saved 
    pdata = pdata + wdata #  Stores a single security code to the pdata  Variable 
  file.close() #  Close a file 
  print("\033[1;31m" + pdata + "\033[0m") #  The screen outputs the generated anti-counterfeiting code information 
  if typeis != "no": #  Whether to display the message prompt box of "Output Complete". If typeis The value of is " no " , Display without showing 
    #  Displays the message prompt box of "Output Complete". The display information contains the saving path of the anti-counterfeiting information code 
    tkinter.messagebox.showinfo(" Prompt ", smsg + str(len(randstr)) + "\n  Storage location of anti-counterfeiting code file: " + datafile)
    root.withdraw() #  Close auxiliary window 


More readers interested in Python can check the topics of this site: "Summary of Python Coding Operation Skills", "Summary of Python Picture Operation Skills", "Python Data Structure and Algorithm Tutorial", "Summary of Python Socket Programming Skills", "Summary of Python Function Use Skills", "Summary of Python String Operation Skills", "Introduction and Advanced Classic Tutorial of Python" and "Summary of Python File and Directory Operation Skills"

I hope this article is helpful to everyone's Python programming.


Related articles: