python模板代码生成_基于模板的简易代码生成器Python源码 | 学步园

需要写的单元测试太多,框架又是类似的,但类名和变量名却各不相同。索性花几分钟用Python脚本写个简易代码生成器,顺便记录于此,得空优化一下,以备后用。

代码

import os

import datetime

tplFilePath = 'E://template.java'

path = 'E://'

testObjList = ['Deposit',\

'Config',\

'FundsFilter',\

'MobileOpenAuth',\

'MobilePartnerRiskLevel',\

'MobilePhoneChargeOrder',\

'MobileSmspayValicode',\

'MobileSystemParam',\

'MobileVd',\

'Order',\

'OriginalOrder',\

'Package',\

'Plantform',\

'Seq',\

'Service',\

'TipsDelivery',\

'Trustlogin',\

'UpdateConfig',\

'UpdateHint',\

'UserAccount',\

'UserDao',\

'UserStatus'\

]

for testObj in testObjList:

testObjVarName = testObj[0].lower() + testObj[1:]

filename = 'Ibatis' + testObj +'DAOTester.java'

author = 'chenxin'

now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

tplFile = open(tplFilePath)

gFile = open(path+filename ,"w")

fileList = tplFile.readlines()

for fileLine in fileList:

line = fileLine.replace('${author}',author)\

.replace('${now}',now)\

.replace('${testObject}',testObj)\

.replace('${testObjVarName}',testObjVarName)

print line

gFile.writelines(line)

tplFile.close()

gFile.close()

模板

/**

* created since ${now}

*/

package com.alipay.mspcore.common.dal.ibatis;

import java.util.Date;

import junit.framework.Assert;

import com.alipay.mspcore.common.dal.daointerface.${testObject}DAO;

import com.alipay.mspcore.common.dal.dataobject.${testObject};

import com.alipay.sofa.runtime.test.AnnotatedAutowireSofaTestCase;

import com.iwallet.biz.common.util.money.Money;

/**

* @author ${author}

* @version $Id: Ibatis${testObject}DAOTester.java, v 0.1 ${now} ${author} Exp $

*/

public class Ibatis${testObject}DAOTester extends AnnotatedAutowireSofaTestCase {

@Override

public String[] getConfigurationLocations() {

return new String[] { "META-INF/spring/common-dal-db.xml",

"META-INF/spring/mobilespcore-common-dal-dao.xml", "META-INF/spring/common-dal.xml" };

}

@Override

public String[] getResourceFilterNames() {

return new String[] { "META-INF/spring/common-dal-db.xml" };

}

@Override

public String[] getWebServiceConfigurationLocations() {

return new String[] {};

}

private ${testObject}DAO get${testObject}DAO() {

${testObject}DAO dao = (${testObject}DAO) this.getBean("${testObjVarName}DAO", ${testObject}DAO.class, null);

return dao;

}

public void test${testObject}DAO() {

${testObject}DAO configDAO = get${testObject}DAO();

Assert.assertNotNull(configDAO);

}

public void test() {

${testObject}DO ${testObjVarName}DO = new ${testObject}DO();

${testObjVarName}DO.setGmtCreate(new Date());

${testObjVarName}DO.setGmtModified(new Date());

${testObjVarName}DO.setId(10000);

${testObject}DAO ${testObjVarName}DAO = get${testObject}DAO();

long result = ${testObjVarName}DAO.insert(${testObjVarName}DO);

Assert.assertTrue(result > 0);

}

}

效果

/**

* created since 2011-12-30 14:31:38

*/

package com.alipay.mspcore.common.dal.ibatis;

import java.util.Date;

import junit.framework.Assert;

import com.alipay.mspcore.common.dal.daointerface.UpdateHintDAO;

import com.alipay.mspcore.common.dal.dataobject.UpdateHint;

import com.alipay.sofa.runtime.test.AnnotatedAutowireSofaTestCase;

import com.iwallet.biz.common.util.money.Money;

/**

* @author chenxin

* @version $Id: IbatisUpdateHintDAOTester.java, v 0.1 2011-12-30 14:31:38 chenxin Exp $

*/

public class IbatisUpdateHintDAOTester extends AnnotatedAutowireSofaTestCase {

@Override

public String[] getConfigurationLocations() {

return new String[] { "META-INF/spring/common-dal-db.xml",

"META-INF/spring/mobilespcore-common-dal-dao.xml", "META-INF/spring/common-dal.xml" };

}

@Override

public String[] getResourceFilterNames() {

return new String[] { "META-INF/spring/common-dal-db.xml" };

}

@Override

public String[] getWebServiceConfigurationLocations() {

return new String[] {};

}

private UpdateHintDAO getUpdateHintDAO() {

UpdateHintDAO dao = (UpdateHintDAO) this.getBean("updateHintDAO", UpdateHintDAO.class, null);

return dao;

}

public void testUpdateHintDAO() {

UpdateHintDAO configDAO = getUpdateHintDAO();

Assert.assertNotNull(configDAO);

}

public void test() {

UpdateHintDO updateHintDO = new UpdateHintDO();

updateHintDO.setGmtCreate(new Date());

updateHintDO.setGmtModified(new Date());

updateHintDO.setId(10000);

UpdateHintDAO updateHintDAO = getUpdateHintDAO();

long result = updateHintDAO.insert(updateHintDO);

Assert.assertTrue(result > 0);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值