Skip to content

Instantly share code, notes, and snippets.

@yahyaKacem
Created December 29, 2013 13:51
Show Gist options
  • Save yahyaKacem/8170675 to your computer and use it in GitHub Desktop.
Save yahyaKacem/8170675 to your computer and use it in GitHub Desktop.
Convert camel-case to snake-case in python. e.g.: CamelCase -> snake_case e.g.: snake_case -> CamelCase e.g.: CamelCase -> dash-case e.g.: dash-case -> CamelCase By: Jay Taylor [@jtaylor] Me<modifier>: Yahya Kacem <[email protected]> Original gist: https://gist.github.com/jaytaylor/3660565
#!/usr/bin/env python
"""
Convert camel-case to snake-case in python.
e.g.: CamelCase -> snake_case
e.g.: snake_case -> CamelCase
e.g.: CamelCase -> dash-case
e.g.: dash-case -> CamelCase
By: Jay Taylor [@jtaylor]
Me<modifier>: Yahya Kacem <[email protected]>
Original gist: https://gist.github.com/jaytaylor/3660565
"""
import re
class Converter:
def __init__(self):
self._first_cap_re = re.compile(r'(.)([A-Z][a-z]+)')
self._all_cap_re = re.compile('([a-z0-9])([A-Z])')
def camel_to_snake(self, camelCasedStr):
"""
This function converts to snake_case from camelCase
"""
sub1 = self._first_cap_re.sub(r'\1_\2', camelCasedStr)
snake_cased_str = self._all_cap_re.sub(r'\1_\2', sub1).lower()
return snake_cased_str.replace('__', '_')
def camelToDash(self, camelCasedStr):
"""
This function converts to dashed_case from camelCase
"""
sub2 = self._first_cap_re.sub(r'\1-\2', camelCasedStr)
dashed_case_str = self._all_cap_re.sub(r'\1-\2', sub2).lower()
return dashed_case_str.replace('--', '-')
def snakeToCamel(self, snake_cased_str):
return self._convertToCamel(snake_cased_str, "_")
def dashToCamel(self, snake_cased_str):
return self._convertToCamel(snake_cased_str, "-")
def _convertToCamel(self, snake_cased_str, separator):
components = snake_cased_str.split(separator)
preffix = ""
suffix = ""
if components[0] == "":
components = components[1:]
preffix = separator
if components[-1] == "":
components = components[:-1]
suffix = separator
if len(components) > 1:
camelCasedStr = components[0].lower()
for x in components[1:]:
if x.isupper() or x.istitle():
camelCasedStr += x
else:
camelCasedStr += x.title()
else:
camelCasedStr = components[0]
return preffix + camelCasedStr + suffix
#!/usr/bin/env python
"""
Convert camel-case to snake-case in python.
e.g.: CamelCase -> snake_case
e.g.: snake_case -> CamelCase
e.g.: CamelCase -> dash-case
e.g.: dash-case -> CamelCase
By: Jay Taylor [@jtaylor]
Me<modifier>: Yahya Kacem <[email protected]>
Original gist: https://gist.github.com/jaytaylor/3660565
"""
import Converter as C
Converter = C.Converter()
def test_camel_to_snake():
assert Converter.camel_to_snake('snakesOnAPlane') == 'snakes_on_a_plane'
assert Converter.camel_to_snake('SnakesOnAPlane') == 'snakes_on_a_plane'
assert Converter.camel_to_snake('_Snakes_On_APlane_') == '_snakes_on_a_plane_'
assert Converter.camel_to_snake('snakes_on_a_plane') == 'snakes_on_a_plane'
assert Converter.camel_to_snake('IPhoneHysteria') == 'i_phone_hysteria'
assert Converter.camel_to_snake('iPhoneHysteria') == 'i_phone_hysteria'
assert Converter.camel_to_snake('iPHONEHysteria') == 'i_phone_hysteria'
assert Converter.camel_to_snake('_iPHONEHysteria') == '_i_phone_hysteria'
assert Converter.camel_to_snake('iPHONEHysteria_') == 'i_phone_hysteria_'
print "all camelCase to snake_case tests passed"
def testCamelToDash():
assert Converter.camelToDash('snakesOnAPlane') == 'snakes-on-a-plane'
assert Converter.camelToDash('SnakesOnAPlane') == 'snakes-on-a-plane'
assert Converter.camelToDash('-Snakes-On-APlane-') == '-snakes-on-a-plane-'
assert Converter.camelToDash('snakes-on-a-plane') == 'snakes-on-a-plane'
assert Converter.camelToDash('IPhoneHysteria') == 'i-phone-hysteria'
assert Converter.camelToDash('iPhoneHysteria') == 'i-phone-hysteria'
assert Converter.camelToDash('iPHONEHysteria') == 'i-phone-hysteria'
assert Converter.camelToDash('-iPHONEHysteria') == '-i-phone-hysteria'
assert Converter.camelToDash('iPHONEHysteria-') == 'i-phone-hysteria-'
print "all camelCase to dash-case tests passed"
def testSnakeToCamel():
assert Converter.snakeToCamel('_snakes_on_a_plane_') == '_snakesOnAPlane_'
assert Converter.snakeToCamel('snakes_on_a_plane') == 'snakesOnAPlane'
assert Converter.snakeToCamel('Snakes_on_a_plane') == 'snakesOnAPlane'
assert Converter.snakeToCamel('snakesOnAPlane') == 'snakesOnAPlane'
assert Converter.snakeToCamel('I_phone_hysteria') == 'iPhoneHysteria'
assert Converter.snakeToCamel('i_phone_hysteria') == 'iPhoneHysteria'
assert Converter.snakeToCamel('i_PHONE_hysteria') == 'iPHONEHysteria'
assert Converter.snakeToCamel('_i_phone_hysteria') == '_iPhoneHysteria'
assert Converter.snakeToCamel('i_phone_hysteria_') == 'iPhoneHysteria_'
print "all snake_case to camelCase tests passed"
def testDashToCamel():
assert Converter.dashToCamel('-snakes-on-a-plane-') == '-snakesOnAPlane-'
assert Converter.dashToCamel('snakes-on-a-plane') == 'snakesOnAPlane'
assert Converter.dashToCamel('Snakes-on-a-plane') == 'snakesOnAPlane'
assert Converter.dashToCamel('snakesOnAPlane') == 'snakesOnAPlane'
assert Converter.dashToCamel('I-phone-hysteria') == 'iPhoneHysteria'
assert Converter.dashToCamel('i-phone-hysteria') == 'iPhoneHysteria'
assert Converter.dashToCamel('i-PHONE-hysteria') == 'iPHONEHysteria'
assert Converter.dashToCamel('-i-phone-hysteria') == '-iPhoneHysteria'
assert Converter.dashToCamel('i-phone-hysteria-') == 'iPhoneHysteria-'
print "all dash-case to camelCase tests passed"
def run_tests():
test_camel_to_snake()
print "################################################################"
testCamelToDash()
print "################################################################"
testSnakeToCamel()
print "################################################################"
testDashToCamel()
print "################################################################"
if __name__ == '__main__':
print "################################################################"
print "################################################################"
run_tests()
print 'All tests passed.'
print "################################################################"
print "################################################################"
@somada141
Copy link

+1 on @mbylstra suggestion, I realise we don't have to turn Python into JS where each single-liner becomes its own dependency but some things just deserve to be

@studgeek
Copy link

The Inflection library handle this case and is already in PIP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment