[转]用Python做一个自动生成读表代码的小脚本
写在开始(本片文章不是写给小白的,至少你应该知道一些常识!)
大家在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)
伸手党请看下面,大神们误喷,请绕行。
- 本文固定链接: http://4past.net/?p=48
- 转载请注明: X先生 2015年09月02日 于 X先生 发表
[转]用Python做一个自动生成读表代码的小脚本的更多相关文章
- 设计一个自动生成棋盘格子的JS小程序
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- python写一个能生成三种一句话木马的脚本
代码: import time import os from threading import Thread import optparse def aspyijuhua(): try: juy=op ...
- 用 Python 为接口测试自动生成用例
用Python为接口自动生成测试用例 基于属性的测试会产生大量的.随机的参数,特别适合为单元测试和接口测试生成测试用例 尽管早在2006年haskell语言就有了QuickCheck来进行" ...
- 做一个自动修改本机IP和mac的bat文件
原文:做一个自动修改本机IP和mac的bat文件 1.ip bat修改理论探讨 前两天我突然萌生了一个念头:能不能做一个小程序来实现自动配置或修改IP和mac,达到一键搞定的目的,这样尤其适合那些带着 ...
- Python写一个自动点餐程序
Python写一个自动点餐程序 为什么要写这个 公司现在用meican作为点餐渠道,每天规定的时间是早7:00-9:40点餐,有时候我经常容易忘记,或者是在地铁/公交上没办法点餐,所以总是没饭吃,只有 ...
- 用Python做一个知乎沙雕问题总结
用Python做一个知乎沙雕问题总结 松鼠爱吃饼干2020-04-01 13:40 前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以 ...
- 使用python做一个IRC在线下载器
使用python做一个IRC在线下载器 1.开发流程 2.软件流程 3.开始 3.0 准备工作 3.1寻找API接口 3.2 文件模块 3.2.1 选择文件弹窗 3.2.2 提取文件名 3.2.2.1 ...
- 自动生成Code First代码
自动生成Code First代码 在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Fram ...
- hibernate中.hbm.xml和注解方式自动生成数据表的简单实例(由新手小白编写,仅适用新手小白)
绝逼新手小白,so 请大神指点! 如果真的错的太多,错的太离谱,错的误导了其他小伙伴,还望大神请勿喷,大神请担待,大神请高抬贵嘴......谢谢. 好了,正题 刚接触ssh,今天在搞使用.hbm.xm ...
随机推荐
- ActiveMQ(5.10.0) - 使用 JDBC 持久化消息
1. 编辑 ACTIVEMQ_HOME/conf/activemq.xml. <beans> <broker brokerName="localhost" per ...
- Jersey(1.19.1) - Client API, Using filters
Filtering requests and responses can provide useful functionality that is hidden from the applicatio ...
- 海量小文件存储与Ceph实践
海量小文件存储(简称LOSF,lots of small files)出现后,就一直是业界的难题,众多博文(如[1])对此问题进行了阐述与分析,许多互联网公司也针对自己的具体场景研发了自己的存储方案( ...
- Android虚拟机运行问题之小结
首先说一下关于虚拟机的技巧,以前我也总是感觉电脑运行虚拟机的时候老是卡,后来在看课堂直播时看到老师用的是Total Control,在网上搜“Total Control”就能能找到,比系统自带SDK运 ...
- C#学习笔记之线程 - 高级主题:非阻塞同步
非阻塞同步 - Nonblock Synchronization 前面提到,即使在简单的赋值和增加一个字段的情况下也需要处理同步.尽管,使用锁可以完成这个功能,但是锁必定会阻塞线程,需要线程切换,在高 ...
- APC -- Asynchronous Procedure Call 异步过程调用
异步过程调用(APC -- Asynchronous Procedure Call )是一种与常用的和简单的同步对象不同的一种同步机制. 我们在我们线程里使用基本的同步对象如MUTEX去通知其它线程, ...
- C# 3.0 特性之扩展方法
今天,我们来聊一聊C#的扩展方法. C# 3.0中为我们提供了一个新的特性—扩展方法.什么是扩展方法呢?我们看一下MSDN的注解: 扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新 ...
- inline-block元素的一些坑
当年刚知道CSS display有 inline-block 这个神奇的属性的时候,感觉碉堡了,以为从此不用float了,什么div.p,只要 display: inline-block 就在一行上了 ...
- 高性能CSS(二)
避免CSS表达式 CSS表达式是动态设置CSS属性的强大(但危险)方法.Internet Explorer从第5个版本开始支持CSS表达式.下面的例子中,使用CSS表达式可以实现隔一个小时切换一次背景 ...
- Hadoop示例程序WordCount编译运行
首先确保Hadoop已正确安装及运行. 将WordCount.java拷贝出来 $ cp ./src/examples/org/apache/hadoop/examples/WordCount.jav ...