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

大家在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. Eclipse去除jquery引入错误

    之前在写Java项目时,总是出现引入jquery报错,虽然对其方法的应用没有什么影响,但是感觉难受,经过百度得到解决的方法: 第一步:去除eclipse的JS验证:将windows->prefe ...

  2. 面试之C#--垃圾回收器什么时候回收?

    每个对象只有在该对象不存在任何引用才会被垃圾回收起回收. 可以调用静态方法System.GC.Collect()垃圾回收器,但是不建议这么做: 用using语句可以有效的自动释放掉资源. 实在没有办法 ...

  3. webBrowser1_DocumentCompleted不停被调用

    原文地址:http://blog.csdn.net/shuishenlong/article/details/7950576 关于DocumentCompleted事件,MSDN给出的解释是在文档加载 ...

  4. 正则Match

    Match match = Regex.Match("result=23&asdf=asdf", @"result=(\d+)&"); if ( ...

  5. ios开发入门篇(三):UITableView简介

    最近做项目又开始用到了uitableview,温习之余,在这里把uitableview的用法分享一下,有不对的地方欢迎大家提出来. 废话不多说,先创建一个工程,由于Xcode6,去除了创建工程时的空项 ...

  6. 学习 .net 的一些主要网站

    学习 .net 的一些主要网站 来自 http://www.cnblogs.com/trymybest121/articles/500176.html   http://msdn.microsoft. ...

  7. es5 和 es6 class

    // ES5 function User(name,age) { this.name = name; this.age = age; } // 静态方法 User.getClassName = fun ...

  8. OpenJudge 2721 忽略大小写比较字符串大小

    1.Link: http://bailian.openjudge.cn/practice/2721/ 2.Content: 总时间限制: 1000ms 内存限制: 65536kB 描述 一般我们用st ...

  9. rabbitmq+haproxy+keepalived实现高可用集群搭建

    项目需要搭建rabbitmq的高可用集群,最近在学习搭建过程,在这里记录下可以跟大家一起互相交流(这里只是记录了学习之后自己的搭建过程,许多原理的东西没有细说). 搭建环境 CentOS7 64位 R ...

  10. Redirect and POST in ASP.NET

    http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET