Created
August 10, 2016 19:18
-
-
Save jpotts18/f8e1973da5a57178ec6f77a834da4fcc to your computer and use it in GitHub Desktop.
Camel case to underscore converter
This file contains hidden or 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