53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
|
|
# coding=utf-8
|
||
|
|
|
||
|
|
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")
|
||
|
|
|