Skip to content

Instantly share code, notes, and snippets.

@sezanzeb
Last active January 24, 2021 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sezanzeb/ca1f4ebc36f6f65915b5bbb6385db24e to your computer and use it in GitHub Desktop.
Save sezanzeb/ca1f4ebc36f6f65915b5bbb6385db24e to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
"""Transform all function names to snake case."""
import os
import re
def extract_function_name(line):
"""Get the test function name written in that line or None."""
match = re.findall(r'def (test.+?)\(', line)
return match[0] if len(match) > 0 else None
def is_camel_case(function_name):
"""Check if the function name is in camelCase."""
uppercase = len(re.findall('[A-Z]', function_name)) > 0
underscores = '_' in function_name
return uppercase and not underscores
def transform_snake_case(function_name):
"""Transform a function name to snake_case."""
function_name = re.sub(r'([A-Z])', r'_\g<1>', function_name).lower()
function_name = function_name.replace('l_d_a', 'lda')
function_name = function_name.replace('word2_vec', 'word2vec')
function_name = function_name.replace('token2_id', 'token2id')
return function_name
def map_function_names(file):
"""Find all camelCase function names and their replacements."""
mapping = {}
for line in file.readlines():
function_name = extract_function_name(line)
if function_name is None:
continue
if not is_camel_case(function_name):
continue
snake_cased = transform_snake_case(function_name)
mapping[function_name] = snake_cased
return mapping
def main():
for parent, subdirs, filenames in os.walk('gensim/test'):
# for each test file
for filename in filenames:
if not filename.startswith('test_'):
continue
if not filename.endswith('.py'):
continue
path = os.path.join(parent, filename)
with open(path, 'r') as file:
mapping = map_function_names(file)
file.seek(0, 0)
contents = file.read()
if len(mapping) == 0:
continue
with open(path, 'w') as file:
for search, replace in mapping.items():
print(search, '->', replace)
# testPersistence exists, but also testPersistenceIgnore,
# make sure only complete matches are replaced
contents = contents.replace(
f'def {search}(',
f'def {replace}('
)
file.write(contents)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment