網(wǎng)站首頁 編程語言 正文
遇到的問題
工作中遇到這么一個(gè)事,需要寫很多C++的底層數(shù)據(jù)庫(kù)類,但這些類大同小異,無非是增刪改查,如果人工來寫代碼,既費(fèi)力又容易出錯(cuò);而借用python的代碼自動(dòng)生成,可以輕松搞定;?
(類比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ù)的語法方式:
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底層接口類 操作${TABLE_NAME}表
/// TABLE ${TABLE_NAME_UPPER}
/// @author dao_cpp_generator.py
/// @generate date: ${GENE_DATE}
/// [注:本文件為自動(dòng)生成,不需要人為編輯,若有修改,請(qǐng)通過配置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}"; //在數(shù)據(jù)庫(kù)中的表的唯一性標(biāo)識(shí)符
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)
...
python代碼生成程序:?
cpp_generator.py
#! /usr/bin/env python
#coding=utf-8
#Redis底層操作類CPP文件生成程序(*RedisDao.cpp)
#author me115@126.com 2014-7-22
import os,sys,re,traceback
from datetime import datetime
from string import Template
class DaoCppGenerator:
def generate(self):
tableName = 'students'
className = '%sRedisDao' % tableName.capitalize()
filePath = r'include/%s.cpp' % className
class_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.將生成的代碼寫入文件
class_file.writelines(lines)
class_file.close()
print 'generate %s over. ~ ~' % filePath
有了這個(gè)程序,再配合一堆XML配置文件,就可以輕松生成各種C++程序代碼了。
原文鏈接:https://www.cnblogs.com/me115/p/3867382.html
相關(guān)推薦
- 2022-05-06 C#迭代器方法介紹_C#教程
- 2022-11-26 R語言學(xué)習(xí)筆記之plot函數(shù)_R語言
- 2022-07-01 Python判斷Nan值的五種方式小結(jié)_python
- 2022-03-17 總結(jié)C#處理異常的方式_C#教程
- 2022-08-30 python在文件中倒序查找個(gè)關(guān)鍵詞
- 2022-03-12 深入了解C#多線程安全_C#教程
- 2022-10-21 React實(shí)現(xiàn)動(dòng)態(tài)調(diào)用的彈框組件_React
- 2023-01-26 Redis慢查詢?nèi)罩九c監(jiān)視器問題_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支