python 自动生成C++代码 (代码生成器)
python 代碼自動(dòng)生成的方法 (代碼生成器)
遇到的問(wèn)題
工作中遇到這么一個(gè)事,需要寫(xiě)很多C++的底層數(shù)據(jù)庫(kù)類(lèi),但這些類(lèi)大同小異,無(wú)非是增刪改查,如果人工來(lái)寫(xiě)代碼,既費(fèi)力又容易出錯(cuò);而借用python的代碼自動(dòng)生成,可以輕松搞定;
(類(lèi)比JAVA中的Hibernate自動(dòng)生成的數(shù)據(jù)庫(kù)底層操作代碼)
下面介紹使用python字符串替換的方法;
Python字符串替換的幾種方法
1. 字符串替換
將需要替換的內(nèi)容使用格式化符替代,后續(xù)補(bǔ)上替換內(nèi)容;
template = "hello %s , your website is %s " % ("大CC","http://blog.me115.com")
print(template) 也可使用format函數(shù)完成:
template = "hello {0} , your website is {1} ".format("大CC","http://blog.me115.com")
print(template) 注:該方法適用于變量少的單行字符串替換;
2. 字符串命名格式化符替換
使用命名格式化符,這樣,對(duì)于多個(gè)相同變量的引用,在后續(xù)替換只用申明一次即可;
template = "hello %(name)s ,your name is %(name), your website is %(message)s" %{"name":"大CC","message":"http://blog.me115.com"}
print(template) 使用format函數(shù)的語(yǔ)法方式:
template = "hello {name} , your name is {name}, your website is {message} ".format(name="大CC",message="http://blog.me115.com")
print(template) 注:適用相同變量較多的單行字符串替換;
3.模版方法替換
使用string中的Template方法;
from string import Template
tempTemplate = string.Template("Hello $name ,your website is $message")
print(tempTemplate.substitute(name='大CC',message='http://blog.me115.com')) 有了模版方法后,就可以將模版保存到文件單獨(dú)編輯,在生成的地方替換為需要的變量;
示例:代碼生成
這個(gè)示例使用以上講到的第三種方法;
建立一個(gè)模版文件,里面需要替換的內(nèi)容使用${}變量替換;
dao_cpp.template
///
/// @class ${CLASSNAME}
/// @brief Redis底層接口類(lèi) 操作${TABLE_NAME}表
/// TABLE ${TABLE_NAME_UPPER}
/// @author dao_cpp_generator.py
/// @generate date: ${GENE_DATE}
/// [注:本文件為自動(dòng)生成,不需要人為編輯,若有修改,請(qǐng)通過(guò)配置py腳本來(lái)重新生成.]#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}"; //在數(shù)據(jù)庫(kù)中的表的唯一性標(biāo)識(shí)符
const string ${CLASSNAME}::KEY_SEPARETER = "${KEY_SEPARETER}";${CLASSNAME}::${CLASSNAME}(void)
{if ( 0 == m_reHandler.EnsureConnect())m_bRedisConnected = true;elsem_bRedisConnected = false;
}${CLASSNAME}::~${CLASSNAME}(void)
{
}int ${CLASSNAME}::InsertRecord(const string& strVal)
... python代碼生成程序:
cpp_generator.py
#! /usr/bin/env python
#coding=utf-8
#Redis底層操作類(lèi)CPP文件生成程序(*RedisDao.cpp)
#author me115@126.com 2014-7-22
import os,sys,re,traceback
from datetime import datetime
from string import Templateclass DaoCppGenerator:def generate(self):tableName = 'students'className = '%sRedisDao' % tableName.capitalize()filePath = r'include/%s.cpp' % classNameclass_file = open(filePath,'w')lines = []#模版文件template_file = open(r'dao_cpp.template','r')tmpl = Template(template_file.read())#模版替換lines.append(tmpl.substitute(CLASSNAME = className,TABLE_NAME = tableName,TABLE_NAME_UPPER = tableName.upper(), GENE_DATE = datetime.now().strftime('%Y-%m-%d %H:%M:%S'),TABLE_ID = '115',EXPIRE_DATE = '06JUN14'))# 0.將生成的代碼寫(xiě)入文件class_file.writelines(lines)class_file.close()print 'generate %s over. ~ ~' % filePath 有了這個(gè)程序,再配合一堆XML配置文件,就可以輕松生成各種C++程序代碼了;
Posted by: 大CC | 25JUL,2014
博客:blog.me115.com [訂閱]
微博:新浪微博
轉(zhuǎn)載于:https://www.cnblogs.com/me115/p/3867382.html
總結(jié)
以上是生活随笔為你收集整理的python 自动生成C++代码 (代码生成器)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 去丽江发生高原反应的症状,去丽江怎么预防
- 下一篇: 零代价修复海量服务器的内核缺陷——UCl