最近在用python自動生成c++的類.因為這些類會根據需求不同產生不同的類,所以需要用python自動生成.由於會產生大量的類,而且這些類是變化的.所以如果是在某個.h中要用include來加載這些類,會累死人的.所以用python來生成這些類的頭文件引用和類的類名聲明
先看例子,再聊python的讀寫文件的代碼 在聊聊我的python代碼
------------------------>
好吧.上面的圖就是面臨的需求
下面來聊聊從網上找的讀寫文件的python代碼吧.csdn的一個博主寫的很詳細. python如何讀寫文件:http://blog.csdn.net/adupt/article/details/4435615
仔細閱讀完博主的講解后,有這么一段
file_object = open('thefile.txt')try:
all_the_text = file_object.read( )
finally:
file_object.close( )
針對上面的這段代碼我會在來個說明 關於 try except finally with 等的帖子 <python中的 try except finally with>
恩.好了,下面把我的python代碼也就是實現最開始的截圖的代碼放這里 供自己以后類似的情況來參考
1 #!/usr/bin/python #此文件是自動生成頭文件聲明的python腳本 2 # -*- encoding: utf-8 -*- 3 import os 4 5 def generateRecordsHeaderInclude(folderPath): 6 7 try: 8 filePath = os.path.join(folderPath,"misc_records.h") 9 with open(filePath, "w") as file: 10 recordsFolderPath = os.path.join(folderPath, 'records') #E:\fare_uuid\misc\include\misc\records 存放生成的類的.h 11 includeNames = os.listdir(recordsFolderPath) #獲取records文件夾下的所有文件名 12 #寫c++代碼 13 file.write("#ifndef MISC_MISC_RECORDS_H_\n") 14 file.write("#define MISC_MISC_RECORDS_H_\n") 15 file.write("\n//Records\n") 16 17 for includeName in includeNames: 18 file.write('#include "misc/records/%s"\n' % includeName) 19 20 file.write("#endif /* MISC_MISC_RECORDS_H_ */") 21 except: 22 print "create file %s\\misc_records.h error" % folderPath 23 return 24 25 print "create file %s\\misc_records.h success!" % folderPath 26 27 28 29 if __name__ == '__main__': 30 homedir = r"E:\farestar_uuid\misc" 31 folderPath = os.path.join(homedir, 'include', 'misc') 32 print 'folderPath = ' + folderPath 33 34 generateRecordsHeaderInclude(folderPath) #E:\farestar_uuid\misc\include\misc
1 #!/usr/bin/python #此文件是自動生成c++類的類名聲明的python腳本 2 # -*- encoding: utf-8 -*- 3 import os 4 5 def getStructName(recFilePath, fwdFilePath): 6 try: 7 with open(recFilePath, 'r') as file: 8 for line in file: 9 if 'struct' in line and ': public MiscObject' in line: 10 words = line.split(" ") 11 structIndex = words.index('struct') # 類名里面是 struct xxx : public MiscObject 所以讀到這一行的時候就拆解處 類名來 12 return words[structIndex+1] 13 except: 14 print 'read %s error' % filePath 15 print 'create %s error' % fwdFilePath 16 raise RuntimeError("create %s error in genRecordsStructDeclare.py" % fwdFilePath) 17 18 19 def generateRecordsStructDeclare(folderPath): 20 try: 21 fwdFilePath = os.path.join(folderPath,"misc_fwd.h") #類名聲明的頭文件 22 with open(fwdFilePath, "w") as file: 23 recordsFolderPath = os.path.join(folderPath, 'records') #所以的類名的.h文件在records文件夾下 24 recFileNames = os.listdir(recordsFolderPath) 25 26 27 file.write("#ifndef MISC_MISC_FWD_H_\n") 28 file.write("#define MISC_MISC_FWD_H_\n") 29 30 file.write('#include "misc/miscid.h"\n') 31 file.write('#include "misc/miscerrorcode.h"\n') 32 file.write('namespace misc\n') 33 file.write('{\n') 34 for recFileName in recFileNames: 35 recFilePath = os.path.join(recordsFolderPath, recFileName) #xxxx/recors/agency.h 36 structName = getStructName( recFilePath, fwdFilePath) 37 file.write(' struct %s;\n' % structName) #生成一行類名聲明 38 file.write('}\n') 39 40 #file.write('#include "misc/records/%s"\n' % includeName) 41 42 file.write("#endif /* MISC_MISC_FWD_H_ */") 43 except: 44 print "create file %s error" % fwdFilePath 45 return 46 47 print "create file %s success!" % fwdFilePath 48 49 50 51 if __name__ == '__main__': 52 homedir = r"E:\farestar_uuid\misc" 53 folderPath = os.path.join(homedir, 'include', 'misc') 54 print 'folderPath = ' + folderPath 55 56 generateRecordsHeaderInclude(folderPath)