使用python生成c文件模板
目标
完成一个python脚本,实现指定名字后,自动生成.c和.h的模板。例如:
/** * @file epc.c * @author 陈维 * @version V01 * @date 2017.01.11 * @brief epc芯片驱动 * @note * * @attention COYPRIGHT INMOTION ROBOT */
代码
# ctemplate.pyimport sys import datetimeclass ctemplate:__fileName = ' '__sourceName = ' '__headerName = ' '__time = datetime.datetime.now().strftime('%Y.%m.%d')__fileComments = {'@attention':'COPYRIGHT WEYNE','@note':' ','@brief':' ','@date':__time,'@version':'V01','@author':'Weyne Chen','@file':" ",}__CommentsOrder = ('@file','@author','@version','@date','@brief','@note','@attention')def __init__(self,s):self.__fileName = sself.__sourceName = s + ".c"self.__headerName = s + '.h'def generateCommentsAtTop(self,name):self.__fileComments['@file'] = namecomments = ('/**\n')#find max length of stringmaxLen = 0for s in self.__CommentsOrder:if(len(s) > maxLen):maxLen = len(s)for k in self.__CommentsOrder:alignSpaceAmount = maxLen - len(k) + 4alignSpace = alignSpaceAmount * ' 'comments += ('* '+ k + alignSpace + self.__fileComments[k] + '\n')comments += ('**/\n\n')return commentsdef generateCommentsInTheEnd(self):return ("/********************* (C) COPYRIGHT WEYNE CHEN *******END OF FILE ********/\n")def createSource(self):fh = open(self.__sourceName,mode = 'w',encoding='utf-8')cm = self.generateCommentsAtTop(self.__sourceName)cm += ("#include \"%s\"\n" %self.__headerName) cm += ("\n"*5)cm += self.generateCommentsInTheEnd()fh.write(cm)fh.close()def createHeader(self):fh = open(self.__headerName,mode = 'w',encoding='utf-8')cm = self.generateCommentsAtTop(self.__headerName)cm += "#ifndef __%s_H\n" %self.__fileName.upper()cm += "#define __%s_H\n" %self.__fileName.upper()cm += ("\n"*5)cm += "#endif\n"cm += self.generateCommentsInTheEnd()fh.write(cm)fh.close()def createTemplatePairs(self):self.createSource()self.createHeader()if __name__ == '__main__':if len(sys.argv) != 2:sys.stderr.write("please input corret parameter")else:s = sys.argv[1]ct = ctemplate(s)ct.createTemplatePairs()
在CMD中执行 python ctemplate.py test
之后,会生成test.c和test.h文件,并已经添加部分描述内容。效果如下:
test.c
/** * @file test.c * @author Weyne Chen * @version V01 * @date 2017.01.19 * @brief * @note * @attention COPYRIGHT WEYNE **/#include "test.h"/********************* (C) COPYRIGHT WEYNE CHEN *******END OF FILE ********/
test.h
/** * @file test.h * @author Weyne Chen * @version V01 * @date 2017.01.19 * @brief * @note * @attention COPYRIGHT WEYNE **/#ifndef __TEST_H #define __TEST_H#endif /********************* (C) COPYRIGHT WEYNE CHEN *******END OF FILE ********/
总结
需要注意python字典在迭代器输出时,不是按输入顺序输出的。因为其存储过程实际是散列的,所以输出的顺序是不定的。若是想要顺序输出,只能使用list或者给dict增加一个tuple,并将key值存储到tuple中。
转载于:https://www.cnblogs.com/WeyneChen/p/6670599.html
使用python生成c文件模板相关推荐
- python数据生成pdf,Python生成pdf文件的方法
摘要:这篇Python开发技术栏目下的"Python生成pdf文件的方法",介绍的技术点是"python生成pdf文件.python生成pdf.生成pdf文件.Pytho ...
- Python 生成 GIF 文件
Python 生成 GIF 文件 使用 Python 合成 gif 动态图,程序如下: 原图片需具有相同大小 import imageio import cv2 import osdef compre ...
- python生成迁移文件
ubuntu 20.04b版本,python生成迁移文件时,即python manage.py makemigrations命令 提示"No changes detected",分 ...
- Python 生成大量文件及大文件
Python 生成大量文件及大文件 很多时候测试需要大量的文件和很大的文件,这些手动操作是非常麻烦的,现在来看看使用python自动生成 一次生成大量小文件(速度很慢) import os impor ...
- Python生成exe文件的方法
python生成exe文件的方法: 一.安装 pyinstaller pip install pyinstaller 二.使用 pyinstaller 命令 # 打包成exe,并设置图标 pyinst ...
- python生成exe文件运行闪退解决方法
** python生成exe文件运行闪退解决方法: ** 使用pyinstaller生成.exe文件: pyinstaller -F filename.py 用python写了一个程序,在python ...
- python生成json文件
@python生成json文件 python生成json文件 import json control={"bw":100,"delay": "10ms ...
- python生成pyc文件可以互用吗_Python生成pyc文件
Python生成pyc文件 pyc文件是py文件编译后生成的字节码文件(byte code).pyc文件经过python解释器最终会生成机器码运行.所以pyc文件是可以跨平台部署的,类似Java的.c ...
- python根据模板生成pdf文件_使用Python生成pdf文件
Python平台的优秀PDF报表类库Reportlab.它不属于Python的标准类库,所以必须手动下载类库包并安装: yum install python-reportlab -y 这篇文章将介绍r ...
- python生成表格文件_python 读取excel文件生成sql文件实例详解
python 读取excel文件生成sql文件实例详解 学了python这么久,总算是在工作中用到一次.这次是为了从excel文件中读取数据然后写入到数据库中.这个逻辑用java来写的话就太重了,所以 ...
最新文章
- windows系统下Python环境的搭建
- 使用C#+Linq+SQL快速开发业务
- CSDN内容颜色、位置以及图片大小改变
- Apache CXF实现WebService发布和调用
- 笔记-信息系统安全管理-信息安全保障系统
- vue 心跳监控_vue websocket 加心跳
- python json dumps utf8_Python2操作JSON出现乱码的解决方案
- django内置服务器
- SNI: 实现多域名虚拟主机的SSL/TLS认证
- firework.js
- 1996年考研数学一解析pdf
- 龙芯3A3000上实现BLFS的轻量级桌面LXDE
- 30分钟了解DNS及搭建方法
- error: Exited sync due to fetch errors
- 网秦安全盾 原理分析
- 远程VPS和本地电脑之间无法粘贴复制及分享文件
- js 按照中文拼音来排序
- AtCoder Beginner Contest 236 A-D题解
- Android 12.0 系统多个播放器app时,设置默认播放器
- 蓝牙模块——基础知识介绍