I want to convert db columns such as is_deleted or product_code which consist of xx_xx to java-style names: isDeleted or productCode. I wonder if there is a way to improve my following code, using regex or such things.

def getJavaName(column):                                                         
   words = column.split('_')                                                    
   first = words[0]                                                             
   return first + ''.join((w.capitalize() for w in words[1:]))   
  • \$\begingroup\$ Since this is under the code review section, I'll provide the answer I would in a real code review. Don't do it at all. Doing a refactoring of naming in this way has a fair chance of introducing bugs with no improvement in functionality. AKA: If it ain't broke don't fix it. Spend the time you save working on new features or fixing real problems. \$\endgroup\$ – user69264 Mar 30 '15 at 16:24
  • \$\begingroup\$ @user69264 Thanks for practical suggestions. Actually,I wrote this script replace an Eclipse plugin used in my work, also for purpose——leaning python :) \$\endgroup\$ – Mark Soul Apr 2 '15 at 5:21
  • \$\begingroup\$ Actually this type of name refactoring has been going on for years in the Client-Server, SOA, and ETL spaces. It is not so much to rename columns or alter DDL, but to create mappings of the objects as structs and such. So if I am working in C, I want the naming conventions and familiar look and feel. This matters still as there is a lot of Java-DB2 going on. Not to mention Django. \$\endgroup\$ – mckenzm May 18 '15 at 1:29

You should remove the many trailing spaces.

You can replace slicing with unpacking and the double-brackets can be removed:

def getJavaName(column):
   first, *rest = column.split('_')
   return first + ''.join(word.capitalize() for word in rest)

Python uses snake_case, even if your DB columns don't, so you should be calling this get_java_name - although personally it's still better named snake_to_camel_case and the column parameter should be renamed to, say, name.

  • 1
    \$\begingroup\$ I like the way you are doing the extraction. nice. \$\endgroup\$ – bhathiya-perera Mar 29 '15 at 14:21
  • \$\begingroup\$ first, *rest = column.split('_') doesn't work in Python 2.x. \$\endgroup\$ – Nizam Mohamed Mar 29 '15 at 17:35
  • \$\begingroup\$ @NizamMohamed you can use first, rest = column.split('')[0], column.spilt('')[1:] in Python 2.x \$\endgroup\$ – Mark Soul Mar 30 '15 at 5:12
  • \$\begingroup\$ Thanks for this, neat little trick for manipulating the result array \$\endgroup\$ – Daniel Dubovski Apr 29 '19 at 12:25

Using regex,

import re
def getJavaName(column):
    return re.sub('_.',lambda x: x.group()[1].upper(),column)

Consider writing this as a one-liner regular expression substitution instead.

import re

def get_java_name(column_name):
    return re.sub('_([a-z])', lambda match: match.group(1).upper(), column_name)

Since the function is Python code, it should still adhere to PEP 8 naming conventions — get_java_name() instead of getJavaName().

  • \$\begingroup\$ This fails for non-ASCII identifiers. You might argue that it's bad practice anyway, though ;). \$\endgroup\$ – Veedrac Mar 29 '15 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.