从用python自动生成.h的头文件集合和类声明集合到用python读写文件
最近在用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代码也就是实现最开始的截图的代码放这里 供自己以后类似的情况来参考
#!/usr/bin/python #此文件是自动生成头文件声明的python脚本
# -*- encoding: utf-8 -*-
import os def generateRecordsHeaderInclude(folderPath): try:
filePath = os.path.join(folderPath,"misc_records.h")
with open(filePath, "w") as file:
recordsFolderPath = os.path.join(folderPath, 'records') #E:\fare_uuid\misc\include\misc\records 存放生成的类的.h
includeNames = os.listdir(recordsFolderPath) #获取records文件夹下的所有文件名
#写c++代码
file.write("#ifndef MISC_MISC_RECORDS_H_\n")
file.write("#define MISC_MISC_RECORDS_H_\n")
file.write("\n//Records\n") for includeName in includeNames:
file.write('#include "misc/records/%s"\n' % includeName) file.write("#endif /* MISC_MISC_RECORDS_H_ */")
except:
print "create file %s\\misc_records.h error" % folderPath
return print "create file %s\\misc_records.h success!" % folderPath if __name__ == '__main__':
homedir = r"E:\farestar_uuid\misc"
folderPath = os.path.join(homedir, 'include', 'misc')
print 'folderPath = ' + folderPath generateRecordsHeaderInclude(folderPath) #E:\farestar_uuid\misc\include\misc
#!/usr/bin/python #此文件是自动生成c++类的类名声明的python脚本
# -*- encoding: utf-8 -*-
import os def getStructName(recFilePath, fwdFilePath):
try:
with open(recFilePath, 'r') as file:
for line in file:
if 'struct' in line and ': public MiscObject' in line:
words = line.split(" ")
structIndex = words.index('struct') # 类名里面是 struct xxx : public MiscObject 所以读到这一行的时候就拆解处 类名来
return words[structIndex+1]
except:
print 'read %s error' % filePath
print 'create %s error' % fwdFilePath
raise RuntimeError("create %s error in genRecordsStructDeclare.py" % fwdFilePath) def generateRecordsStructDeclare(folderPath):
try:
fwdFilePath = os.path.join(folderPath,"misc_fwd.h") #类名声明的头文件
with open(fwdFilePath, "w") as file:
recordsFolderPath = os.path.join(folderPath, 'records') #所以的类名的.h文件在records文件夹下
recFileNames = os.listdir(recordsFolderPath) file.write("#ifndef MISC_MISC_FWD_H_\n")
file.write("#define MISC_MISC_FWD_H_\n") file.write('#include "misc/miscid.h"\n')
file.write('#include "misc/miscerrorcode.h"\n')
file.write('namespace misc\n')
file.write('{\n')
for recFileName in recFileNames:
recFilePath = os.path.join(recordsFolderPath, recFileName) #xxxx/recors/agency.h
structName = getStructName( recFilePath, fwdFilePath)
file.write(' struct %s;\n' % structName) #生成一行类名声明
file.write('}\n') #file.write('#include "misc/records/%s"\n' % includeName) file.write("#endif /* MISC_MISC_FWD_H_ */")
except:
print "create file %s error" % fwdFilePath
return print "create file %s success!" % fwdFilePath if __name__ == '__main__':
homedir = r"E:\farestar_uuid\misc"
folderPath = os.path.join(homedir, 'include', 'misc')
print 'folderPath = ' + folderPath generateRecordsHeaderInclude(folderPath)
从用python自动生成.h的头文件集合和类声明集合到用python读写文件的更多相关文章
- 利用python自动生成verilog模块例化模板
一.前言 初入职场,一直忙着熟悉工作,就没什么时间更新博客.今天受“利奇马”的影响,只好宅在家中,写写技术文章.芯片设计规模日益庞大,编写脚本成了芯片开发人员必要的软技能.模块端口动不动就几十上百个, ...
- Python自动生成代码工具
项目中有一个需求,对一个基类而言,拥有一个比较方法和拷贝方法,某些地方需要频繁地对这两个方法进行调用.对于所有子类而言,需要重写这两个方法,并在其中维护类内一些成员变量.例如有一个变量m_iMyVal ...
- python 自动生成C++代码 (代码生成器)
python 代码自动生成的方法 (代码生成器) 遇到的问题 工作中遇到这么一个事,需要写很多C++的底层数据库类,但这些类大同小异,无非是增删改查,如果人工来写代码,既费力又容易出错:而借用pyth ...
- 使用python自动生成docker nginx反向代理配置
由于在测试环境上用docker部署了多个应用,而且他们的端口有的相同,有的又不相同,数量也比较多,在使用jenkins发版本的时候,不好配置,于是想要写一个脚本,能在docker 容器创建.停止的时候 ...
- python自动生成bean类
近期在学习python,一直在和java做对比,目前没有发现有通过字段自动生成getter setter方法,故此自己写了一个类,可以通过__init__方法传入类名和字段数组,再调用内部的方法,就可 ...
- python笔记 利用python 自动生成条形码 二维码
1. ean13标准条形码 from pystrich.ean13 import EAN13Encoder encode = EAN13Encoder(') encode.save('d:/barco ...
- python 自动生成model 文件 案例分析
生成方式 Python中想要自动生成 model文件可以通过 sqlacodegen这个命令来生成对应的model文件 sqlacodegen 你可以通过pip去安装: pip install sql ...
- 利用Python自动生成暴力破解的字典
Python是一款非常强大的语言.用于测试时它非常有效,因此Python越来越受到欢迎. 因此,在此次教程中我将聊一聊如何在Python中生成字典,并将它用于任何你想要的用途. 前提要求 1,Pyth ...
- 小工具:使用Python自动生成MD风格链接
很久之前我在Github上搞了一个LeetCode的仓库,但一直没怎么维护.最近发现自己刷了不少LC的题目了,想搬运到这个仓库上. 玩Github最重要的当然是写README了,MD的逼格决定了项目牛 ...
随机推荐
- Eclipse用法和技巧十二:快速复制一行
写代码的总有那么个时候需要Ctrl+c,Ctrl+v,在eclipse中如果遇到需要快速在一行代码的上下复制一行的话,有很方便的快捷键可以使用.将光标放到某一行,按住Ctrl+Alt+Down,即可以 ...
- Java类加载机制深度分析
转自:http://my.oschina.net/xianggao/blog/70826 参考:http://www.ibm.com/developerworks/cn/java/j-lo-class ...
- js弹出层插件 -- weebox
1.引入文件: 使用前需包含以下jquery.js.bgiframe.js.weebox.js.wee.css文件 2.基本用法举例如下: <script type="text/jav ...
- C# 自定义事件
C#自定义事件和java有所不同,涉及到委托.下面代码包括自定义事件从事件定义到事件触发和执行的全过程. using System; using System.Collections.Generic; ...
- 找出数组中出现奇数次的元素<异或的应用>
点击打开链接:百度面试题之找出数组中之出现一次的两个数(异或的巧妙应用) 题目描述|:给定一个包含n个整数的数组a,其中只有一个整数出现奇数次,其他整数都出现偶数次,请找出这个整数 使用异或操作,因为 ...
- Entity FrameWork 指导文章
Entity FrameWork学习指导: 转:http://www.entityframeworktutorial.net/code-first/configure-many-to-many-rel ...
- sift算法c语言实现
前段时间在做三维測量方面的研究.须要得到物体表面三维数据.sift算法是立体匹配中的经典算法.以下是对RobHess的SIFT源码的凝视.部分内容參考网上,在这里向各位大神表示感谢. http://b ...
- WPF – 使用触发器
WPF – 使用触发器 WPF提供了很重要的一个东西就是绑定Binding, 它帮助我们做了很多事情,这个我们在WPF学习之绑定这篇里边有讲过.对于Binding我们可以设置其绑定对象,关系,并通过某 ...
- RedHat7安装Sublime Text 3
下载Sublime Text 3 # wget http://c758482.r82.cf2.rackcdn.com/sublime_text_3_build_3083_x64.tar.bz2 解压S ...
- jquery自调用匿名函数解析
alert("undefined" in window); (function (window, undefined) { //构造jQuery ...