Skip to content

Instantly share code, notes, and snippets.

@stfuchs
Last active November 30, 2018 01:32
Show Gist options
  • Save stfuchs/074b6bf9b2b24f77d54ac6b293388620 to your computer and use it in GitHub Desktop.
Save stfuchs/074b6bf9b2b24f77d54ac6b293388620 to your computer and use it in GitHub Desktop.
Convert between camel and snake
# Credit:
# https://stackoverflow.com/questions/19053707/converting-snake-case-to-lower-camel-case-lowercamelcase
# https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case/32064723
import re
def camel_to_snake(s):
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', re.sub('(.)([A-Z][a-z]+)', r'\1_\2', s)).lower()
def snake_to_camel(s):
return "".join((x.capitalize() for x in s.split('_')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment