歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

基於模板的簡易代碼生成器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);
    }
}

Python向PHP發起GET與POST請求 http://www.linuxidc.com/Linux/2014-10/107903.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved