Skip to content

Instantly share code, notes, and snippets.

@dongho-jung
Created September 20, 2017 07:21
Show Gist options
  • Save dongho-jung/c33f6eab690b35e9b459b280848ab126 to your computer and use it in GitHub Desktop.
Save dongho-jung/c33f6eab690b35e9b459b280848ab126 to your computer and use it in GitHub Desktop.
class for general code refactoring
import re
class CodeRefactor():
def __init__(self):
pass
def __pos2keyRepl(self, m, maxArgs=32):
args = funcList[m.group(1)]
sub = [ args[key]+'='+val for val, key in zip(m.groups()[1:], range(0,32)) ]
return m.group(1)+'('+','.join(sub)+')'
def pos2key(self, fileList, funcList, namingFunc , maxArgs=32):
funcPatternList = ['({})\({}\)'.format(func, ('(.+?),'*len(funcList[func]))[:-1] ) for func in funcList]
for file in fileList:
with open(file, 'r') as f:
new_content = f.read()
for funcPattern in funcPatternList:
new_content = re.sub(funcPattern, self.__pos2keyRepl, new_content)
with open(namingFunc(file), 'w') as f2:
f2.write(new_content)
fileList = ['main.py']
funcList = {
'SSH': ['hostname','username','password'],
'CommandGenerator': ['id','pw','address']
}
a = CodeRefactor()
#a.pos2key(fileList ,funcList, lambda x: x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment