zoukankan      html  css  js  c++  java
  • 【转载】 python 生成C++模板代码

    转载:https://blog.csdn.net/weixin_30245867/article/details/98950730
    转载:https://blog.csdn.net/phunxm/article/details/34917371

    背景:

    在开发过程中,会出现一些 统一代码结构的写法;这些类大同小异,如果人工来写代码,既费力又容易出错;而借用python的代码自动生成,可以轻松搞定;

    模版方法替换

    使用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'))
    

    有了模版方法后,就可以将模版保存到文件单独编辑,在生成的地方替换为需要的变量;

    示例:代码生成

    建立一个模版文件,里面需要替换的内容使用${}变量替换;
    dao_cpp.template

    ///
    /// @class ${CLASSNAME}
    /// @brief Redis底层接口类 操作${TABLE_NAME}表
    /// TABLE ${TABLE_NAME_UPPER}
    /// @author dao_cpp_generator.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)
    ...
    

    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())
     
            #模版替换
            # substitute 会报错 没有匹配到的数值;safe_substitute 会将没有匹配到的数据 原封不动展示出来
            lines.append(tmpl.safe_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()
    

    python字符串模板中的KeyError

  • 相关阅读:
    vim跳转(一)
    代理服务器
    python类
    python 删除/查找重复项
    DNS缓存
    DNS查询过程
    http返回头中content-length与Transfer-Encoding: chunked的问题释疑
    jquery mouseover与mouseenter,mouserout与mouseleave的区别
    php提示Call-time pass-by-reference has been deprecated in的解决方法
    uboot启动内核(3)
  • 原文地址:https://www.cnblogs.com/q1076452761/p/15608414.html
Copyright © 2011-2022 走看看