convert database_column_names to mixedCaseNames and back again

posted June 14th 2006 at 0633 EDT in All, Programming, Python

It's common to name your database columns like this_is_a_column with what I call "underscore case". It's also twice as common to name variables in your code with either CamelCaseFormat or mixedCaseFormat. I hate the tedious effort of going between the two, so here is a snippet of python that does the work for me.

It works by passing a column name to a transform function that processes it through a regular expression. That regular expression passes the matched groups through a helper function before putting it all back together again.

File:caseTransformations.py


import re
def toMixedCase(columnName):
    """ pass 'a_column_name' get back 'aColumnName' """
    return re.sub("(_[a-zA-Z])",toMixedCaseHelper,columnName)
def toMixedCaseHelper(match):
    """ pass a match of '_a' and you get back 'A' """
    return match.group(0)[1].upper()

def toUnderscoreCase(columnName):
    """ pass 'aColumnName' and get back 'a_column_name' """
    return re.sub("([a-z][A-Z])",toUnderscoreCaseHelper,columnName)
def toUnderscoreCaseHelper(match):
    """ pass a match of 'aZ' and get back 'a_z' """
    return match.group(0)[0] + '_'+match.group(0)[1].lower()

 
Now let's see it at work:

>>> import caseTransformations
>>> caseTransformations.toMixedCase("your_column_name")
'yourColumnName'
>>> caseTransformations.toUnderscoreCase("yourMixedCaseName")
'your_mixed_case_name'

Beautiful.

It might not be perfect, and you could speed it up by pre-compiling the regular expressions, but I'm happy with it (and open to suggestions of a better implementation)

5 Responses

  1. #1 Anonymous
    4 years, 1 month ago

    def toMixedCase(s):
    words = s.lower().split(“_”)
    return words[0] + “”.join(map(str.capitalize, words[1:]))

  2. #2 Anonymous
    4 years, 1 month ago

    doesn’t the python style guide PEP suggest using thiskindof_naming for variables and object attributes?

  3. #3 jehiah
    4 years, 1 month ago

    @Anonymous #1 : Nice function i’ll have to test the two to see which is faster… but i’d guess it’s not mine.

    @Anonymous #2: The style guide does indeed suggest that, however, for me the real world is a different place and I end up goin by style guides other than the Style Guide for Python Code which you referenced.

  4. #4 David
    4 years, 1 month ago

    Congratulations–this article got picked up by the Daily PythonURL!

  5. #5 Brian
    4 years ago

    Might suggest using a lambda to eliminate the helper functions:

    def toUnderscoreCase(s):
    helper = lambda match: match.group(0)[0] + ‘_’ + match.group(0)[1].lower()
    return re.sub(“([a-z][A-Z])”, helper, s)