contexts
In the course of writing the code, if there isFrequent repetitive coding operationsOr you canReuseThe various types of code can be written in Python by writing a script thatAutomatic generation of such codeSo you don't have to handwrite, or copy, every time.
For example, a new fixed code framework, add some established software logic, communication protocols, message templates, etc., and then write a set of code, or a Function, each time to make the script through the one-click code generation, there is no need to write again each time, at the same time you can put the relevant software logic, but also to avoid errors.
script code
Demo_CodeGenerator.py
Specific details of the code removed, you want to generate what kind of code in mycode append the appropriate line, and then tap Display to enter the relevant parameters, you can automatically generate the code file.
#conding=utf-8 from FileHandler import WritetoFile import sys PwdPath = [0] class BuildCode_Dev: 'Auto Generate code of Device control' def __init__(self, KeyWord = 'TestDemoCommand'): = KeyWord def Generate(self): fileName = 'Code_Dev.txt' mycode = [] # Append the appropriate line in mycode to whatever code you want to generate. ('\n---------------------- Demo code Below: ---------------------- ') ('\n---------------------- Demo code Below: ---------------------- ') ('\n---------------------- Demo code Below: ---------------------- ') ('\n***** DemoCode_Get_DataLength() ***** ') ('\n***** DemoCode_Set_DataLength() ***** ') ('Switch ('+ + '):') (' case('+ + '):') (' break;') WritetoFile(fileName,mycode) print('Code:'+ + ' Generator OK!') return(mycode) if __name__ == '__main__': if([1:] == []): print('Not input parameter , Use Test Data') CmdKeyWord = 'TestDemoCommand' else: CmdKeyWord = [1] #code = BuildCode_Dev(CmdKeyWord) #() #print(PwdPath) #print(CmdKeyWord)
Mainly through the tkinter graphical display, according to the input parameters, click on the button to generate the corresponding code.
# -*- coding: utf-8 -*- from tkinter import * from GatewayControl_CodeGenerator import * from DeviceControl_CodeGenerator import * def GWMsg(): (1.0,END) Input = str((1.0,END)) Cmd_Gw = BuildCode_GW(Input) CodeRst = Cmd_Gw.Generate() s = 'Code_GW.txt Generate Success:\n\n' (END, s) (END, CodeRst) #(0.0, END) def DevMsg(): (1.0,END) Input = str((1.0,END)) Cmd_Dev = BuildCode_Dev(Input) CodeRst = Cmd_Dev.Generate() s = 'Code_Dev.txt Generate Success:\n\n' (END, s) (END, CodeRst) #(0.0, END) def clearContent(): (1.0, END) (1.0,END) root = Tk() ('1000x600') (' Code Generator') (bg='#f0ffff') #Lable intro = Label(root,text='Please enter the message/command name on the left, then select the appropriate button to generate the code',\ bg='#d3fbfb',\ fg='red',\ font=('Chinese New Wei',11),\ width=20,\ height=2,\ relief=RIDGE) (relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1) #Input inputData = Text(root, font = ('',14)) (relx=0.1, rely=0.2, relwidth=0.3, relheight=0.6) #Output txt = Text(root, font = ('',9)) (relx=0.6, rely=0.2, relwidth=0.3, relheight=0.6) #Button bt_json2bin = Button(root, text='**Demo Control', command=GWMsg, fg ='blue') bt_json2bin.place(relx=0.4, rely=0.25, relwidth=0.2, relheight=0.1) bt_bin2json = Button(root, text='**Demo Control', command=DevMsg, fg ='blue') bt_bin2json.place(relx=0.4, rely=0.45, relwidth=0.2, relheight=0.1) bt_clear = Button(root, text='Clear', command=clearContent, fg ='blue') bt_clear.place(relx=0.4, rely=0.65, relwidth=0.2, relheight=0.1) intro = Label(root,text='The full code generated is in the Code_GW.txtmaybeCode_Dev.txtcenter(current catalog ), Contact person for questions:Howard',\ bg='#d3fbfb',\ fg='red',\ font=('Chinese New Wei',11),\ width=20,\ height=2,\ relief=RIDGE) (relx=0.1, rely=0.8, relwidth=0.8, relheight=0.1) ()
:
The main thing is to write the code to a file to save it
def WritetoFile(FileName,Data): with open(FileName,'w') as record: strr = "\n" content = (Data) (content+'\n')
Script run results:
Running, after selecting the corresponding button, the corresponding generated code py file will be called.
Finally this can be packaged into an exe for easy use.
Script Code Catalog
This is the whole content of this article.