Created
February 11, 2013 14:08
-
-
Save dsuch/4754601 to your computer and use it in GitHub Desktop.
A Python function to uncamelify CamelCaseNames
This file contains 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
# Use it under the Python license | |
import re | |
_uncamelify_re = re.compile(r'((?<=[a-z])[A-Z]|(?<!\A)[A-Z](?=[a-z]))') | |
# Inspired by http://stackoverflow.com/a/9283563 | |
def uncamelify(s, separator='-', elem_func=unicode.lower): | |
""" Converts a CamelCaseName into a more readable one, e.g. | |
will turn ILikeToReadWSDLDocsNotReallyNOPENotMeQ into | |
i-like-to-read-wsdl-docs-not-really-nope-not-me-q or a similar one, | |
depending on the value of separator and elem_func. | |
""" | |
return separator.join(elem_func(elem) for elem in re.sub(_uncamelify_re, r' \1', s).split()) | |
original = u'ILikeToReadWSDLDocsNotReallyNOPENotMeQ' | |
expected1 = 'i-like-to-read-wsdl-docs-not-really-nope-not-me-q' | |
expected2 = 'I_LIKE_TO_READ_WSDL_DOCS_NOT_REALLY_NOPE_NOT_ME_Q' | |
assert uncamelify(original) == expected1 | |
assert uncamelify(original, '_', unicode.upper) == expected2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the re.VERBOSE version: