【Python自动化】编程控制类型变量顺序化
2016-07-30 11:12
344 查看
比如:
有这么一个文件,这是一个例子。
因为编程过程中间又插入一个消息类型,那么此时又要修改后面的数据顺序化,非常繁琐,
python实现自动顺序化:
上面通过匹配每个消息,进行值顺序化,代码非常简单。
python是自动化的利器。
#ifndef MSGTYPE_H #define MSGTYPE_H // login const short MSG_LOGIN = 8; // register const short MSG_REGIS = 22; // send message to friend const short MSG_SENDTOFRIEND = 2; // send message to group const short MSG_SENDTOGROUP = 3; // add friend const short MSG_ADDFRIEND = 4; const short MSG_ADDFRIEND_WAIT = 9; const short MSG_ADDFRIEND_NONAME = 6; // initial client's friend list const short MSG_INITFRIEND_ONLINE = 7; const short MSG_INITFRIEND_OFFLINE = 88; const short MSG_INITGROUP = 11; // 当本地没有检测到好友等初始化数据,那么服务器给予相关数据 const short MSG_BUILDGROUP = 10; // invite member const short MSG_GROUPINVIMEM = 12; const short MSG_GROUPINVIMEM_WAIT = 18; const short MSG_GROUPINVIMEM_SUCC = 13; const short MSG_GROUPINVIMEM_FAIL = 14; const short MSG_GROUPINVIMEM_NONAME = 15; // request enter group const short MSG_REQENTERGROUP = 16; const short MSG_REQENTERGROUP_WAIT = 17; const short MSG_REQENTERGROUP_SUCC = 1; const short MSG_REQENTERGROUP_FAIL = 19; const short MSG_REQENTERGROUP_NONAME = 20; const short MSG_DOWNLOAD_FILE = 21; const short MSG_UPLOAD_FILE = 64; ////////////////////////////// /////////reload控制信息////////////// const short MSG_RELOAD_FILE = 23; ////////////////////////////// const short MSG_DELETE_FILE = 24; const short MSG_ECHO = 25; const short MSG_OK = 26; const short MSG_ERROR = 27; const short MSG_NOT_CLEAR = 28; #endif // MSGTYPE_H
有这么一个文件,这是一个例子。
因为编程过程中间又插入一个消息类型,那么此时又要修改后面的数据顺序化,非常繁琐,
python实现自动顺序化:
1 #!/usr/bin/python 2 3 import re 4 import sys 5 import os 6 7 def SerializeNumber(): 8 fd = open(sys.argv[1], 'r') 9 fd_1 = open("swap", "w") 10 lines = fd.readlines() 11 ls = [] 12 i = 0 13 j = 0 14 print lines 15 for line in lines: 16 if re.match("const short", line): 17 result = re.match("const short \w+_\w+_?\w* ?= ?", line).group(); 18 lines[i] = result + str(j) + ";" 19 print lines[i] 20 j+=1 21 ls.append(lines[i].strip('\n') + '\n') 22 i += 1 23 24 fd_1.writelines(ls) 25 fd.close() 26 fd_1.close() 27 os.remove(sys.argv[1]) 28 os.rename("swap", sys.argv[1]) 29 30 31 if __name__ == '__main__': 32 SerializeNumber()
上面通过匹配每个消息,进行值顺序化,代码非常简单。
python是自动化的利器。
相关文章推荐
- JavaScript(一)--变量和数据类型/流程控制语句/函数/网页时钟/基于对象编程
- Python编程-编码、变量、数据类型
- Python学习 第4天 基本语法-变量、数据类型、运算符、流程控制
- Python--前端基础之JavaScript(JS的引入方式,JS的变量、常量和标识符,JS的数据类型,运算符,流程控制,JavaScript的对象)
- python 编程从入门到实践 第二章 变量和简单数据类型
- python基础-变量、数据类型、input、格式化、运算符、流程控制
- python 变量,数据类型,用户输入输出,流程控制,循环
- python 元类型编程,实现匿名验证器的装饰器AuthenticationDecoratorMeta
- Python核心编程 检查类型脚本的一个小问题
- 004--从C#到Python —— 1 变量和数据类型
- Windows 下的Python环境变量控制
- VC编程常见类型变量 ZT(http://www.vckbase.com/study/article/data_convert.htm)
- Java基础学习笔记(一)环境变量配置,数据类型,运算符,控制语句,循环语句
- C#编程入门之类型和变量
- python实现自动化远程控制(2)--构造抽象类和接口
- Python tkinter 包含的插件类型 & tkinter中可变的变量
- Python:pygame游戏编程之旅三(玩家控制的小球)
- python中如何判断一个变量的数据类型?(原创)
- Python变量和对象类型速记手册
- python中如何判断一个变量的数据类型