我有一个python项目,其函数定义用CamelCase样式编写。 我正在尝试编写一个脚本来将它们转换为snake_case样式。
CaseFormatter类
import re
class CaseFormatter:
def __init__(self, file_directory):
self.first_cap_re = re.compile('(.)([A-Z][a-z]+)')
self.all_cap_re = re.compile('([a-z0-9])([A-Z])')
self.functions_dict = {}
self.file_directory = file_directory
self.file = open(file_directory, "r", encoding="UTF-8")
self.file_content = self.file.read()
self.names_dictionary = {}
def convert_camel_case_to_snake_case(self, name):
"""this function convert a camel case name to a snake case name """
s1 = self.first_cap_re.sub(r'\1_\2', name)
self.names_dictionary[name] = self.all_cap_re.sub(r'\1_\2', s1).lower()
def find_camel_case_functions(self):
"""this function finds camel case functions in a file """
s1 = re.findall("^\s*def (\S+)\s*\(\s*\S+\s*(?:,\s*\S+)*\):$", self.file_content)
return s1
def replace_functions(self):
# file_content = open(self.file_directory, "r", encoding="UTF-8").read()
self.file_content = self.file_content.replace(";", "")
for key, val in self.names_dictionary.items():
self.file_content = self.file_content.replace(key, val)
# print(self.file_content)
self.file = open(self.file_directory, "w", encoding="UTF-8")
print(self.file_content)
self.file.write(self.file_content)
测试CaseFormatter类
import os
from CaseFormatter import *
walk_dir = 'some dirctory'
print('walk_dir = ' + walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))
for root, subDirs, files in os.walk(walk_dir):
print('--\nroot = ' + root)
for filename in files:
file_path = os.path.join(root, filename)
if filename.endswith('.py') and filename != "__init__.py":
print('\t- file %s (full path: %s)' % (filename, file_path))
case_formatter = CaseFormatter(file_path)
# print(case_formatter.find_camel_case_functions())
for function_name in case_formatter.find_camel_case_functions():
case_formatter.convert_camel_case_to_snake_case(function_name)
print(case_formatter.names_dictionary)
case_formatter.replace_functions()
我发现RegEx可以找到函数定义here。 当我在我的项目上尝试它时,它没有给我任何结果,RegEx没有像我想的那样工作。 作为项目中某个文件的示例:
class UnvoweledPattern(object):
String = ''
Rules = []
IDs = []
def __init__(self, string, rules, ids):
self.String = string
self.Rules = rules
self.IDs = ids
pass
def GetRootsStringsAndRules(self, string):
if (string == None):
string = self.String
rootStrings = []
rootRules = []
for j in range(len(self.Rules)):
rootRule = ''
rootString = ''
for k in range(len(self.Rules[j])):
rootRule += self.Rules[j][k]
if self.Rules[j][k].isdigit():
rootString += string[int(self.Rules[j][k]) - 1]
else:
rootString += self.Rules[j][k]
rootStrings.append(rootString)
rootRules.append(rootRule)
return [rootStrings, rootRules]