从用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的逼格决定了项目牛 ...
随机推荐
- php如何修改SESSION的生存时间
如何修改SESSION的生存时间 我们来手动设置 Session 的生存期: <?phpsession_start(); // 保存一天 $lifeTime = 24 * 3600; setco ...
- mvn开发可执行的java程序
1. 用maven-assembly-plugin插件 2. 在项目的pom文件中加入以下该插件的配置 <span style="font-size:18px;">& ...
- ios 利用Reveal来调试界面2--真机调试(步骤详解)
使用真机调试我们的App界面,如果你的真机是没有越狱的设备,那么使用Reveal来调试UI的步骤是最麻烦的.
- 指向函数的指针 分类: C/C++ 2015-07-13 11:03 14人阅读 评论(0) 收藏
原文网址:http://www.cnblogs.com/zxl2431/archive/2011/03/25/1995285.html 讲的很清楚,备份记录. (一) 用函数指针变量调用函数 可以用指 ...
- Android(java)学习笔记188:关于构造代码块,构造函数的一道面试题(华为面试题)
1.源码是: package text; public class TestStaticCon { public static int a = 0; static { a = 10; System.o ...
- asp.net 使用IHttpModule 做权限检查 登录超时检查(转)
IHttpModule 权限 检查 登录超时检查 这样就不需要每个页面都做一次检查 也不需要继承任何父类. using System;using System.Collections.Generic; ...
- 3、bootstrap3.0 栅格偏移 布局中的一个特产
理解了栅格化,那么栅格偏移也相对容易理解了.v3的偏移分别有以下几种: offset:左外边距(margin-left): pull:右位移(right): push:左位移(left). 其中off ...
- java问题:类的定义,对象的定义?
java问题:类的定义,对象的定义? 类是一组数据和函数的集合,只是抽象的概念,它的作用就是生成对象,它生成对象后,就为这个对象分了一块存储区,类可以生成无限多个对象,每个对象都有自己的存储区,在类里 ...
- 学习java随笔第八篇:封装、继承、多态
java和c#一样都是面向对象的语言. 面向对象的语言有三大特征:封装.继承.多态 封装 封装:隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别. class Perso ...
- 学习java随笔第六篇:数组
一维数组 创建一维数组并输出 public class OneDimensionalArray { public static void main(String argas[]) { int i[]= ...