Converting Between Different Naming Conventions
Credit: Sami Hangaslammi
Problem
You have a body of code whose identifiers use one of the common naming conventions to represent multiple words in a single identifier (CapitalizedWords, mixedCase, or under_scores), and you need to convert the code to another naming convention in order to merge it smoothly with other code.
Solution
re.sub covers the two hard cases,
converting underscore to and from the others:
import re
def cw2us(x): # capwords to underscore notation
return re.sub(r'(?<=[a-z])[A-Z]|(?<!^)[A-Z](?=[a-z])',
r"_\g<0>", x).lower( )
def us2mc(x): # underscore to mixed-case notation
return re.sub(r'_([a-z])', lambda m: (m.group(1).upper( )), x)Mixed-case to underscore is just like capwords to underscore (the case-lowering of the first character becomes redundant, but it does no harm):
def mc2us(x): # mixed-case to underscore notation
return cw2us(x)Underscore to capwords can similarly exploit the underscore to mixed-case conversion, but it needs an extra twist to uppercase the start:
def us2cw(x): # underscore to capwords notation
s = us2mc(x)
return s[0].upper( )+s[1:]Conversion between mixed-case and capwords is, of course, just an issue of lowercasing or uppercasing the first character, as appropriate:
def mc2cw(x): # mixed-case to capwords
return s[0].lower( )+s[1:]
def cw2mc(x): # capwords to mixed-case
return s[0].upper( )+s[1:]Discussion
Here are some usage examples:
>>> cw2us("PrintHTML")
'print_html' >>> cw2us("IOError") ...