Skip to content

Instantly share code, notes, and snippets.

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