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

大家在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. Codevs 3729==洛谷P1941 飞扬的小鸟

    P1941 飞扬的小鸟 456通过 2.4K提交 题目提供者该用户不存在 标签动态规划2014NOIp提高组 难度提高+/省选- 提交该题 讨论 题解 记录   题目描述 Flappy Bird 是一 ...

  2. 【转】【CDC翻客】移动端App测试实用指南

     译者注:本文从测试人员的角度出发,提出了100多个在测试移动App过程中需要考虑的问题.不管你是测试人员.开发.产品经理或是交互设计师,在进行移动App开发时,这些问题都很有参考价值.我和Queen ...

  3. MyBatis(3.2.3) - Configuring MyBatis using XML, typeHandlers

    As discussed in the previous chapter, MyBatis simplifies the persistent logic implementation by abst ...

  4. MyBatis(3.2.3) - Handling the CLOB/BLOB types

    MyBatis provides built-in support for mapping CLOB/BLOB type columns. Assume we have the following t ...

  5. sqlserver 关于快照

    数据库快照:是数据库某一时间点的视图,快照涉及最初目的是为了报表服务,快照还可以和镜像结合来达到读写分离的目的 数据库快照:是sqlserver数据库的只读静态视图快照的作用:1 提供了一个静态的视图 ...

  6. plsqldev与sqldeveloper

    plsqldev连接 1.连接不同服务器,要修改tnsnames.ora文件,具体如下修改如下位置 # tnsnames.ora Network Configuration File: \app\us ...

  7. ios开发:GCD多线程

    ios有三种多线程编程技术,分别是NSThread,Cocoa NSOperation和GCD,GCD全称Grand Central Dispatch 是Apple开发的一个多核编程的解决方法,在iO ...

  8. 【墙裂推荐】大学生如何学习WEB开发

    每天网络上有上万条Web招聘职位,招聘要求很简单: 会JavaScript,会CSS,能开发网页,能设计网页. 但我们真正面试时才发现:都是些很小很小的知识点! 我们没有实践过,没有碰到过,头脑一片茫 ...

  9. CSU-ACM2016暑假集训训练1-二分搜索 A - Can you find it?

    Time Limit:3000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description Give yo ...

  10. 关于内存的5个函数(malloc,VirtualAlloc,GlobalAlloc,LocalAlloc,HeapAlloc)

    VirtualAlloc 该函数的功能是在调用进程的虚地址空间,预定或者提交一部分页,如果用于内存分配的话,并且分配类型未指定MEM_RESET,则系统将自动设置为0 一次分配 1PAGE 以上的 R ...