从用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的逼格决定了项目牛 ...
随机推荐
- CUDA8.0+VS2013的安装和配置
首先声明,本文借鉴自:http://blog.csdn.net/u011314529/article/details/51505029 所以,可参考链接的博文.但原文有个瑕疵就是,cublas.lib ...
- 如何解决因为找不到Notepad++的安装路径而导致的不能更新CS-Script的问题
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何解决因为找不到Notepad++的安装路径而导致的不能更新CS-Script的问题.
- 20169210《Linux内核原理与分析》第四周作业
网易云课堂学习: 在实验楼的学习中,这次的实验是利用mykernel实验模拟计算机硬件平台 首先进入linux-3.9.4, $cd LinuxKernel/linux-3.9.4 如下图所示 接下来 ...
- IO负载高的来源定位 IO系列
http://elf8848.iteye.com/category/281637 前言: 在一般运维工作中经常会遇到这么一个场景,服务器的IO负载很高(iostat中的util),但是无法快速的定位到 ...
- qt 程序国际化
http://www.cnblogs.com/hujian/archive/2012/08/10/2631488.html
- python 函数初识和文件操作
文件操作 打开文件:文件句柄 = open('文件路径', '模式') 打开文件的模式 w #以写的方式打开 (不可读,不存在则创建,存在则删除内容) a #以追加的模式打开(可读, 不存在则创建 ...
- (转)Spring 读书笔记-----使用Spring容器(一)
Spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spr ...
- Spring入门Hello World
这里是关于Hello World的一些基本的操作 Spring 是一个重量级的容器框架,用来配置bean并维护bean之间的关系的框架 想要最初的使用Spring就要学会最基本的配置 <1> ...
- python基础知识六
博客园的博文对每篇博文的长度似乎做了限制 面向对象编程, 在程序何种,根据操作数据的函数或语句块来设计程序.这被成为面向过程的编程.还有一种把数据和功能结合起来,用称为对象的东西包裹起来组织组织程序的 ...
- ubuntu下配置java环境变量
1.官网下载linux对应的jdk安装包tar.gz 2.filezilla上传tar.gz到对应ubuntu目录test下(见上一篇) 3.解压:tar -zcvf XXX.tar.gz 4.修改解 ...