Created
August 10, 2016 19:18
-
-
Save jpotts18/aa9b67a3c70b49af78b3701b346b8703 to your computer and use it in GitHub Desktop.
Camel case to underscore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
camel_pat = re.compile(r'([A-Z])') | |
under_pat = re.compile(r'_([a-z])') | |
def camel_to_underscore(name): | |
return camel_pat.sub(lambda x: '_' + x.group(1).lower(), name) | |
def underscore_to_camel(name): | |
return under_pat.sub(lambda x: x.group(1).upper(), name) | |
def convert_json(d, convert): | |
new_d = {} | |
for k, v in d.iteritems(): | |
new_d[convert(k)] = convert_json(v, convert) if isinstance(v, dict) else v | |
return new_d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment