python修改函数命名方式
时间:2011-02-22 来源:ubunoon
#! /usr/bin/env python
#coding=utf-8
# author: ubunoon
# email: netubu # gmail.com
# date: 2011-2-22
# blog: http://www.cnblogs.com/ubunoon # http://www.qtrstudio.com/blog
import re
import shutil
func = re.compile(r'^def\s(\w+)')
name = re.compile(r'^[a-zA-Z]+(_[a-z])*')
def replaceFuncName(func_name):
while '_' in func_name:
value = name.search( func_name )
if value is not None:
k = value.groups()[0]
if k is not None:
func_name = func_name.replace(k, k[1].upper())
return func_name
def processLine(lines):
new_lines = []
for line in lines:
value = func.match(line)
if value is not None:
func_name = value.groups()[0]
value = name.match( func_name )
new_func_name = replaceFuncName( func_name)
line = line.replace( func_name, new_func_name )
new_lines.append(line)
return new_lines
if __name__ == '__main__':
with open('utils.py') as ut:
new_lines = processLine( ut.readlines() )
shutil.copy( 'utils.py', 'utils.py.bak' )
with open('utils.py', 'w') as ut:
ut.writelines( new_lines )
修改正则表达式,则很容易修改其他语言中关于函数的定义和类定义。
# copyright:2011 Copright, All right reserved.
my pearl:
http://zjyuezz.cn.alibaba.com/
相关阅读 更多 +