Skip to content

Commit

Permalink
utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sorl committed Dec 29, 2011
1 parent 5d9e241 commit 7258762
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions profilebase/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import re


uncamel_patterns = (
re.compile('(.)([A-Z][a-z]+)'),
re.compile('([a-z0-9])([A-Z])'),
)


def uncamel(s):
"""
Make camelcase lowercase and use underscores.
>>> uncamel('CamelCase')
'camel_case'
>>> uncamel('CamelCamelCase')
'camel_camel_case'
>>> uncamel('Camel2Camel2Case')
'camel2_camel2_case'
>>> uncamel('getHTTPResponseCode')
'get_http_response_code'
>>> uncamel('get2HTTPResponseCode')
'get2_http_response_code'
>>> uncamel('HTTPResponseCode')
'http_response_code'
>>> uncamel('HTTPResponseCodeXYZ')
'http_response_code_xyz'
"""
for pat in uncamel_patterns:
s = pat.sub(r'\1_\2', s)
return s.lower()

0 comments on commit 7258762

Please sign in to comment.