Skip to content

Instantly share code, notes, and snippets.

@dtolb
Created March 2, 2017 20:59
Show Gist options
  • Select an option

  • Save dtolb/7921f96febd05fe232b6116f08989007 to your computer and use it in GitHub Desktop.

Select an option

Save dtolb/7921f96febd05fe232b6116f08989007 to your computer and use it in GitHub Desktop.
convert comments from camel to snake
import re
def convert_comment_string_to_snake_case(s):
"""
Changes String to from camelCase to snake_case
:param s: String to convert
:rtype: String
:rertuns: String converted to snake_case
"""
# regex = r"(\s+##\s+\'\w+)((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))(\w+\'.+)"
# a = re.compile("(\s+##\s+\'\w+)((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))(\w+\')")
regex = r"(\s+##\s+\"\w+)((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))(\w+\".+)"
#a = re.compile("(\s+##\s+\'\w+)((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))(\w+\')")
# m.group(1)sub(r'_\1', s).lower()
result = re.match(regex, s)
if result:
return result.group(1) + "_" + result.group(2).lower() + result.group(3) + '\n'
else:
return s
with open('bandwidth/catapult/__init__.py') as f:
with open('out2.py', 'w') as e:
for line in f:
e.write(convert_comment_string_to_snake_case(line))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment