A class that has recently generated C + + automatically with Python. Because these classes produce different classes depending on the requirements, they need to be generated automatically with Python. Because there are a lot of classes, and these classes are changed. So if you want to load these classes in an. h with include, It's going to be exhausting. So use Python to generate header file references for these classes and class name declarations for classes
Let's take a look at the example, and then talk about Python's code for reading and writing files talking about my Python code.
------------------------>
All right, the picture above is the face of the demand.
Let's talk about the Python code for reading and writing files from the Internet. A blogger in Csdn wrote it in detail. How Python reads and writes files: http://blog.csdn.net/adupt/article/details/4435615
After carefully reading the blogger's explanation, there is a
File_object = open (' Thefile.txt ')
Try
All_the_text = File_object.read ()
Finally
File_object.close ()
For this code, I'm going to show you a try except finally with> in the post <python about try except finally with, etc.
Well, here's the code for my Python code, which is the implementation of the very beginning, which I'll refer to in a similar situation.
1 #!/usr/bin/python #此文件是自动生成头文件声明的python脚本2 #-*-encoding:utf-8-*-3 ImportOS4 5 defGeneraterecordsheaderinclude (folderpath):6 7 Try:8FilePath = Os.path.join (FolderPath,"Misc_records.h")9With open (FilePath,"W") as file:TenRecordsfolderpath = Os.path.join (FolderPath,'Records') #E: \fare_uuid\misc\include\misc\records stores the. h of the generated class. OneIncludenames =Os.listdir (recordsfolderpath) #获取records文件夹下的所有文件名 A#写c + + code -File.write ("#ifndef misc_misc_records_h_\n") -File.write ("#define Misc_misc_records_h_\n") theFile.write ("\n//records\n") - - forIncludenameinchIncludenames: -File.write ('#include "misc/records/%s" \ n'%includename) + -File.write ("#endif/* Misc_misc_records_h_ * /") + except: A Print "Create file%s\\misc_records.h error"%FolderPath at return - - Print "Create file%s\\misc_records.h success!"%FolderPath - - - in if __name__=='__main__': -Homedir = R"E:\farestar_uuid\misc" toFolderPath = Os.path.join (Homedir,'include','Misc') + Print 'FolderPath ='+FolderPath - theGeneraterecordsheaderinclude (FolderPath) #E: \farestar_uuid\misc\include\misc
1 #python script for class name declaration of!/usr/bin/python #此文件是自动生成c + + class2 #-*-encoding:utf-8-*-3 ImportOS4 5 defgetstructname (Recfilepath, Fwdfilepath):6 Try:7With open (Recfilepath,'R') as file:8 forLineinchFile:9 if 'struct' inchLine and ': Public Miscobject' inchLine :TenWords = Line.split (" ") OneStructindex = Words.index ('struct'# class name inside is struct xxx:public miscobject so when you read this line, you break down the class name to A returnWords[structindex+1] - except: - Print 'Read%s error'%FilePath the Print 'Create%s Error'%Fwdfilepath - RaiseRuntimeError ("Create%s error in genrecordsstructdeclare.py"%Fwdfilepath) - - + defGeneraterecordsstructdeclare (folderpath): - Try: +Fwdfilepath = Os.path.join (FolderPath,"misc_fwd.h") #类名声明的头文件 AWith open (Fwdfilepath,"W") as file: atRecordsfolderpath = Os.path.join (FolderPath,'Records') #所以的类名的. h files under the records folder -Recfilenames =Os.listdir (Recordsfolderpath) - - -File.write ("#ifndef misc_misc_fwd_h_\n") -File.write ("#define Misc_misc_fwd_h_\n") in -File.write ('#include "misc/miscid.h" \ n') toFile.write ('#include "misc/miscerrorcode.h" \ n') +File.write ('namespace misc\n') -File.write ('{\ n') the forRecfilenameinchRecfilenames: *Recfilepath =Os.path.join (Recordsfolderpath, recfilename) #xxxx/recors/agency.h $Structname =getstructname (Recfilepath, Fwdfilepath)Panax NotoginsengFile.write ('struct%s;\n'%structname) #生成一行类名声明 -File.write ('}\n') the + #file.write (' #include ' misc/records/%s "\ n '% includename) A theFile.write ("#endif/* Misc_misc_fwd_h_ * /") + except: - Print "Create file%s error"%Fwdfilepath $ return $ - Print "Create file%s success!"%Fwdfilepath - the - Wuyi if __name__=='__main__': theHomedir = R"E:\farestar_uuid\misc" -FolderPath = Os.path.join (Homedir,'include','Misc') Wu Print 'FolderPath ='+FolderPath - AboutGeneraterecordsheaderinclude (FolderPath)
Collection of header files and class declarations from automatically generated. h to read and write files in Python