11
\$\begingroup\$

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:])) 
Bergi
1,7659 silver badges16 bronze badges
asked Mar 29, 2015 at 13:30
\$\endgroup\$
3
  • \$\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\$ Commented Mar 30, 2015 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\$ Commented Apr 2, 2015 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\$ Commented May 18, 2015 at 1:29

3 Answers 3

12
\$\begingroup\$

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.

answered Mar 29, 2015 at 14:03
\$\endgroup\$
4
  • 1
    \$\begingroup\$ I like the way you are doing the extraction. nice. \$\endgroup\$ Commented Mar 29, 2015 at 14:21
  • \$\begingroup\$ first, *rest = column.split('_') doesn't work in Python 2.x. \$\endgroup\$ Commented Mar 29, 2015 at 17:35
  • \$\begingroup\$ @NizamMohamed you can use first, rest = column.split('')[0], column.spilt('')[1:] in Python 2.x \$\endgroup\$ Commented Mar 30, 2015 at 5:12
  • \$\begingroup\$ Thanks for this, neat little trick for manipulating the result array \$\endgroup\$ Commented Apr 29, 2019 at 12:25
7
\$\begingroup\$

Using regex,

import re
def getJavaName(column):
 return re.sub('_.',lambda x: x.group()[1].upper(),column)
answered Mar 29, 2015 at 18:15
\$\endgroup\$
0
5
\$\begingroup\$

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().

answered Mar 29, 2015 at 18:32
\$\endgroup\$
1
  • \$\begingroup\$ This fails for non-ASCII identifiers. You might argue that it's bad practice anyway, though ;). \$\endgroup\$ Commented Mar 29, 2015 at 19:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.