为了减少重复代码编写的工作量,对常用属性进行自动化更改。采用python编写自动化生成C++代码脚本。
python自动化脚本编写import os,sys,re,traceback
from datetime import datetime
from string import Template
def generate(self):
tableName = 'students'
className = '%sRedisDao' % tableName.capitalize()
filePath = r'%s.cpp' % className
class_file = open(filePath,'w')
lines = []
#模版文件
template_file = open(r'cpp.template','r')
tmpl = Template(template_file.read())
#模版替换
lines.append(tmpl.substitute(
CLASSNAME = className, #类名设置
TABLE_NAME = tableName, #成员变量设置
PRIMER_KEY = 123, #宏定义设置
KEY_SEPARETER = 'stu', #成员变量设置
TABLE_NAME_UPPER = tableName, #未使用
GENE_DATE = datetime.now().strftime('%Y-%m-%d %H:%M:%S'), #代码生成日期设置
TABLE_ID = '115', #成员变量设置
EXPIRE_DATE = '06JUN14')) #未使用
# 0.将生成的代码写入文件
class_file.writelines(lines)
class_file.close()
print('generate %s over. ~ ~' % filePath)
if __name__ == "__main__":
template = "hello %s , your website is %s " % ("大CC","http://blog.me115.com")
print(template)
# 1.0 字符串替换
tmp = "%s is %s" % ("wate", "fire")
print(tmp)
# 2.0 format格式化替换
tp = "it is {name}, the {name} age is {age}".format(name="lihao", age=18)
print(tp)
# 3.0 模板替换
tempTemplate = Template("Hello $name,your website is $message")
print(tempTemplate.substitute(name='CCP',message='http://blog.me115.com'))
# auto generate code
print("generate code start...")
generate("auto_code")
模板C++类编写
创建文件 cpp.template
/// @class ${CLASSNAME}
/// @brief cpp底层接口类 操作${TABLE_NAME}内容
/// TABLE ${TABLE_NAME_UPPER}
/// @author test.py
/// @generate date: ${GENE_DATE}
/// [注:本文件为自动生成,不需要人为编辑,若有修改,请通过配置py脚本来重新生成.]
include “${CLASSNAME}.h”
include “include/${TABLE_NAME}_t.h”
include “RedisManager.h”
include “common/LogMacros.h”
include “common/StringUtility/OtherStringFunc.h”
include “common/DateTime.h”
namespace redisdao{
#define PRIMARY_KEY “${PRIMER_KEY}”
const string ${CLASSNAME}::TABLE_NAME = “${TABLE_NAME}”;
const string ${CLASSNAME}::TABLE_ID = “${TABLE_ID}”; //在数据库中的表的唯一性标识符
const string ${CLASSNAME}::KEY_SEPARETER = “${KEY_SEPARETER}”;
${CLASSNAME}::${CLASSNAME}(void)
{
if ( 0 == m_reHandler.EnsureConnect())
m_bRedisConnected = true;
else
m_bRedisConnected = false;
}
${CLASSNAME}::~${CLASSNAME}(void)
{
}
int ${CLASSNAME}::InsertRecord(const string& strVal)
{
}
执行脚本
python3 test.py
结果如下:
/// @class StudentsRedisDao
/// @brief cpp底层接口类 操作students内容
/// TABLE students
/// @author test.py
/// @generate date: 2022-04-26 14:49:35
/// [注:本文件为自动生成,不需要人为编辑,若有修改,请通过配置py脚本来重新生成.]
include “StudentsRedisDao.h”
include “include/students_t.h”
include “RedisManager.h”
include “common/LogMacros.h”
include “common/StringUtility/OtherStringFunc.h”
include “common/DateTime.h”
namespace redisdao{
#define PRIMARY_KEY “123”
const string StudentsRedisDao::TABLE_NAME = “students”;
const string StudentsRedisDao::TABLE_ID = “115”; //在数据库中的表的唯一性标识符
const string StudentsRedisDao::KEY_SEPARETER = “stu”;
StudentsRedisDao::StudentsRedisDao(void)
{
if ( 0 == m_reHandler.EnsureConnect())
m_bRedisConnected = true;
else
m_bRedisConnected = false;
}
StudentsRedisDao::~StudentsRedisDao(void)
{
}
int StudentsRedisDao::InsertRecord(const string& strVal)
{
}



