最近在用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读写文件的更多相关文章

  1. 利用python自动生成verilog模块例化模板

    一.前言 初入职场,一直忙着熟悉工作,就没什么时间更新博客.今天受“利奇马”的影响,只好宅在家中,写写技术文章.芯片设计规模日益庞大,编写脚本成了芯片开发人员必要的软技能.模块端口动不动就几十上百个, ...

  2. Python自动生成代码工具

    项目中有一个需求,对一个基类而言,拥有一个比较方法和拷贝方法,某些地方需要频繁地对这两个方法进行调用.对于所有子类而言,需要重写这两个方法,并在其中维护类内一些成员变量.例如有一个变量m_iMyVal ...

  3. python 自动生成C++代码 (代码生成器)

    python 代码自动生成的方法 (代码生成器) 遇到的问题 工作中遇到这么一个事,需要写很多C++的底层数据库类,但这些类大同小异,无非是增删改查,如果人工来写代码,既费力又容易出错:而借用pyth ...

  4. 使用python自动生成docker nginx反向代理配置

    由于在测试环境上用docker部署了多个应用,而且他们的端口有的相同,有的又不相同,数量也比较多,在使用jenkins发版本的时候,不好配置,于是想要写一个脚本,能在docker 容器创建.停止的时候 ...

  5. python自动生成bean类

    近期在学习python,一直在和java做对比,目前没有发现有通过字段自动生成getter setter方法,故此自己写了一个类,可以通过__init__方法传入类名和字段数组,再调用内部的方法,就可 ...

  6. python笔记 利用python 自动生成条形码 二维码

    1. ean13标准条形码 from pystrich.ean13 import EAN13Encoder encode = EAN13Encoder(') encode.save('d:/barco ...

  7. python 自动生成model 文件 案例分析

    生成方式 Python中想要自动生成 model文件可以通过 sqlacodegen这个命令来生成对应的model文件 sqlacodegen 你可以通过pip去安装: pip install sql ...

  8. 利用Python自动生成暴力破解的字典

    Python是一款非常强大的语言.用于测试时它非常有效,因此Python越来越受到欢迎. 因此,在此次教程中我将聊一聊如何在Python中生成字典,并将它用于任何你想要的用途. 前提要求 1,Pyth ...

  9. 小工具:使用Python自动生成MD风格链接

    很久之前我在Github上搞了一个LeetCode的仓库,但一直没怎么维护.最近发现自己刷了不少LC的题目了,想搬运到这个仓库上. 玩Github最重要的当然是写README了,MD的逼格决定了项目牛 ...

随机推荐

  1. Android开发之50个常见实用技巧——活用布局

    第一章.活用布局 Hack1. 使用weight属性实现视图的居中显示  ①合用weightSum属性和layout-weight属性 解决问题,如:居中显示按钮,并占据父视图的50%: 代码如: & ...

  2. 加速 Gradle 构建大型 Android 项目的方法[转]

    加速 Gradle 构建大型 Android 项目的方法 时间 2016-03-14 20:38:00  Mystra 原文  http://www.wangchenlong.org/2016/03/ ...

  3. Velocity源码分析

    velocity模板渲染的步骤: 1) 首先初始化启动Velocity引擎,可以通过Velocity.init()或者新建VelocityEngine类,并调用其中的init()方法: 2) 创建一个 ...

  4. GDB 运行PYTHON 脚本+python 转换GDB调用栈到流程图

    http://tromey.com/blog/?cat=17 http://blog.csdn.net/cnsword/article/details/16337031 http://blog.csd ...

  5. hibernate相关知识

    1.为什么要用Hibernate JDBC的优点 直接底层操作,提供了很简单.便捷的访问数据库的方法,跨平台性比较强.灵活性比较强,可以写很复杂的SQL语句. JDBC的缺点 因为JAVA是面向对象的 ...

  6. Coordinate System

    Coordinate System Introduction of Different Coordinate Systems Cartesian Coordinate System UI Coordi ...

  7. Director Scene Layer and Sprite

    Scenes Director Layers Multiple Layers Example: Sprites References Scenes A scene (implemented with ...

  8. (转载)OSI七层参考模型和TCP/IP四层参考模型

    Mallory   网络模型概念浅析 网络模型一般是指OSI七层参考模型和TCP/IP四层参考模型. #只是一种设计==模型# Open System Interconnect的缩写,意为开放式系统互 ...

  9. iOS开发大神必备的Xcode插件

    写在前面 工欲善其事,必先利其器,iOS开发中不仅要学会Xcode的基本操作,而且还得学会一些Xcode的使用技巧,如掌握常用的快捷键等,还有就是今天要说到的Xcode插件,下面我就为大家介绍几款开发 ...

  10. Microsoft Visual Studio Professional 2012 专业版 下载

    记录(以下内容来自网络收集): 下载地址: https://www.microsoft.com/zh-cn/download/details.aspx?id=30682 直接iso连接下载址: htt ...