写在开始(本片文章不是写给小白的,至少你应该知道一些常识!)

大家在Unity开发中,肯定会把一些数据放到配置文件中,尤其是大一点的项目,每次开发一个新功能的时候,都要重复的写那些读表代码。非常烦。来个实用小工具,大家随便看看。

 #-*- coding: utf-8 -*-
#----------------------------------------------------------#
# 版本:python-3.5.0a3-amd64
# 功能:生成读表代码文件
#----------------------------------------------------------#
import os
import sys
import re
import string
import codecs templateFileName = "./template.cs"
outputPathName = "./" def InputFileName2RowClassName(_inputFileName):
_className = ""
_className = "Row_" + _inputFileName.split(".")[0]
return _className def InputFileName2TableClassName(_inputFileName):
_className = ""
_className = "Table_" + _inputFileName.split(".")[0]
return _className def InputFileName2FileName(_inputFileName):
_className = ""
_className = _inputFileName.split(".")[0]
return _className def GenerateRowFields(_type,_name,_desc):
_returnValue = ""
_returnValue += "\t//" + _desc + "\n"
if _type.find("FLOAT")!=-1:
_returnValue += "\tprivate float m_float" + _name + ";\n"
_returnValue += "\tpublic float " + _name + "{get{return m_float" + _name + ";}}\n"
if _type.find("STRING")!=-1:
_returnValue += "\tprivate string m_string" + _name + ";\n"
_returnValue += "\tpublic string " + _name + "{get{return m_string" + _name + ";}}\n"
if _type.find("INT")!=-1:
_returnValue += "\tprivate int m_int" + _name + ";\n"
_returnValue += "\tpublic int " + _name + "{get{return m_int" + _name + ";}}\n"
return _returnValue def GenerateRowFunction(_type,_name,_desc,_i):
_returnValue = ""
if _type.find("FLOAT")!=-1:
_returnValue = "\t\t\tm_float" + _name + " = float.Parse(strCols[i++]);\n"
if _type.find("STRING")!=-1:
_returnValue = "\t\t\tm_string" + _name + " = strCols[i++];\n"
if _type.find("INT")!=-1:
_returnValue = "\t\t\tm_int" + _name + " = Convert.ToInt32(strCols[i++]);\n"
#print(_returnValue)
return _returnValue def GenerateFileds(_inputfilename):
outputText = "";
lineNum = 0
listTypes = []
listNames = []
listDescs = []
textFile = open( _inputfilename, "r", encoding= 'utf-8' )
for line in textFile.readlines():
lineNum = lineNum + 1
#只读前三行
if lineNum <= 3:
#第一行类型
if lineNum == 1:
listTypes = line.strip().split("\t")
#第二行名称
if lineNum == 2:
listNames = line.strip().split("\t")
#第三行注释
if lineNum == 3:
listDescs = line.strip().split("\t")
textFile.close()
for i in range( 0, len(listTypes) ):
outputText += GenerateRowFields(listTypes[i],listNames[i],listDescs[i])
return outputText def GenerateFromText(_inputfilename):
outputText = "";
lineNum = 0
listTypes = []
listNames = []
listDescs = []
textFile = open( _inputfilename, "r", encoding= 'utf-8' )
for line in textFile.readlines():
lineNum = lineNum + 1
#只读前三行
if lineNum <= 3:
#第一行类型
if lineNum == 1:
listTypes = line.strip().split("\t")
#第二行名称
if lineNum == 2:
listNames = line.strip().split("\t")
#第三行注释
if lineNum == 3:
listDescs = line.strip().split("\t")
textFile.close()
for j in range( 0, len(listTypes) ):
#print(listTypes[j],listNames[j],listDescs[j],j)
outputText += GenerateRowFunction(listTypes[j],listNames[j],listDescs[j],j)
return outputText #根据TXT生成客户端代码文件
def GenarateCodeByText(_inputfilename):
#templateFileName = "template.cs"
templateFile = open( templateFileName, "r" )
templateText = templateFile.read()
templateText = templateText.replace( "#{RowClass}", InputFileName2RowClassName(_inputfilename) )
templateText = templateText.replace( "#{Fileds}", GenerateFileds(_inputfilename) )
templateText = templateText.replace( "#{FromText}", GenerateFromText(_inputfilename) )
templateText = templateText.replace( "#{TableClass}", InputFileName2TableClassName(_inputfilename) )
templateText = templateText.replace( "#{FileName}", InputFileName2FileName(_inputfilename) )
templateFile.close()
return templateText #自动写文件
def AutoWriteToFile(_inputfilename):
outputFileName = outputPathName + InputFileName2TableClassName(_inputfilename) + ".cs"
outputText = GenarateCodeByText(_inputfilename).encode("utf-8")
outputFile = open( outputFileName, "wb" )
outputFile.write(outputText)
outputFile.close() #获取文件夹下所有文件路径
def walk(path):
f1 = os.listdir(path)
for f in f1:
#如果是子目录,则递归遍历
if f.find(".svn") == -1 and os.path.isdir(os.path.join(path,f)):
#walk(os.path.join(path,f))
None
else:
if f.find(".txt") != -1:
t_name = os.path.join(path,f)
listfilepath.append(t_name)
return listfilepath listfilepath = [] if __name__ == '__main__':
if len(sys.argv) == 1:
listfilepath = walk("./")
print(len(listfilepath))
for files in listfilepath:
files = files.split("/")[-1]
print(files)
AutoWriteToFile(files)
sys.exit(0)

伸手党请看下面,大神们误喷,请绕行。

code

[转]用Python做一个自动生成读表代码的小脚本的更多相关文章

  1. 设计一个自动生成棋盘格子的JS小程序

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. python写一个能生成三种一句话木马的脚本

    代码: import time import os from threading import Thread import optparse def aspyijuhua(): try: juy=op ...

  3. 用 Python 为接口测试自动生成用例

    用Python为接口自动生成测试用例 基于属性的测试会产生大量的.随机的参数,特别适合为单元测试和接口测试生成测试用例 尽管早在2006年haskell语言就有了QuickCheck来进行" ...

  4. 做一个自动修改本机IP和mac的bat文件

    原文:做一个自动修改本机IP和mac的bat文件 1.ip bat修改理论探讨 前两天我突然萌生了一个念头:能不能做一个小程序来实现自动配置或修改IP和mac,达到一键搞定的目的,这样尤其适合那些带着 ...

  5. Python写一个自动点餐程序

    Python写一个自动点餐程序 为什么要写这个 公司现在用meican作为点餐渠道,每天规定的时间是早7:00-9:40点餐,有时候我经常容易忘记,或者是在地铁/公交上没办法点餐,所以总是没饭吃,只有 ...

  6. 用Python做一个知乎沙雕问题总结

    用Python做一个知乎沙雕问题总结 松鼠爱吃饼干2020-04-01 13:40 前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以 ...

  7. 使用python做一个IRC在线下载器

    使用python做一个IRC在线下载器 1.开发流程 2.软件流程 3.开始 3.0 准备工作 3.1寻找API接口 3.2 文件模块 3.2.1 选择文件弹窗 3.2.2 提取文件名 3.2.2.1 ...

  8. 自动生成Code First代码

    自动生成Code First代码 在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Fram ...

  9. hibernate中.hbm.xml和注解方式自动生成数据表的简单实例(由新手小白编写,仅适用新手小白)

    绝逼新手小白,so 请大神指点! 如果真的错的太多,错的太离谱,错的误导了其他小伙伴,还望大神请勿喷,大神请担待,大神请高抬贵嘴......谢谢. 好了,正题 刚接触ssh,今天在搞使用.hbm.xm ...

随机推荐

  1. HttpClient(4.3.5) - HTTP Protocol Interceptors

    The HTTP protocol interceptor is a routine that implements a specific aspect of the HTTP protocol. U ...

  2. Warrior!之家与Warrior!博客网站发布

    这个网站从大一下学期就开始做,断断续续,一开始感觉无从下手,做了一个草稿便停止了.最近再拿回来,感觉并没有什么难度,便把它做完了,采用ajax页内跳转,对几个搜索引擎都有seo,目前谷歌搜索“Warr ...

  3. winform中文本框的一些案例

    项目中经常看到在输入金额时,会加逗号,最近在复习正则表达式,就联系下,界面如下:

  4. Appcn 移动开发 前台与服务器数据交互

    第一次写.嘿嘿. 言归正传,这几天开始学习移动开发,使用的是Appcan平台.Appcan平台采用HTML5+CSS3做开发 实现跨平台,正好可以满足我们的业务需求. Appacn和数据库进行交互的方 ...

  5. A标签执行js 代码和跳转

    5.执行JS代码: <a href="javascript:js代码">内容</a> ⑥.使用js来实现空链接 写法:<a href="ja ...

  6. From MSI to WiX, Part 8 - Major Upgrade, by Alex Shevchuk

    Following content is reprinted from here, please go to the original website for more information. Au ...

  7. java-多线程-join函数

    join()>>不带参数 线程A调用线程B.join,意思就是线程A并入了线程B,当执行完线程B,再去执行线程A后续动作 join(int keepTims)>>带参数,与上面 ...

  8. Adobe Illustrator CS6 绿色简体中文版下载地址

    一.Adobe Illustrator CS6 简体中文精简绿色优化版:1.由官方简体中文正式版制作而成,只需要执行一次快速安装即可使用.已经注册,非tryout版,支持x64位系统.2.精简了Ext ...

  9. jquery ListBox 左右移动

    <head runat="server"> <title>无标题页</title> <script type="text/jav ...

  10. 短租app简析

    本人应聘某短租app产品经理时做的材料,贴出来请高手指教. 所有内容来自公开资料,不涉及商业秘密.