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– user692642015年03月30日 16:24:32 +00:00Commented 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\$Mark Soul– Mark Soul2015年04月02日 05:21:57 +00:00Commented 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\$mckenzm– mckenzm2015年05月18日 01:29:14 +00:00Commented May 18, 2015 at 1:29
3 Answers 3
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\$JaDogg– JaDogg2015年03月29日 14:21:23 +00:00Commented Mar 29, 2015 at 14:21
-
\$\begingroup\$
first, *rest = column.split('_')
doesn't work in Python 2.x. \$\endgroup\$Nizam Mohamed– Nizam Mohamed2015年03月29日 17:35:17 +00:00Commented Mar 29, 2015 at 17:35 -
\$\begingroup\$ @NizamMohamed you can use first, rest = column.split('')[0], column.spilt('')[1:] in Python 2.x \$\endgroup\$Mark Soul– Mark Soul2015年03月30日 05:12:12 +00:00Commented Mar 30, 2015 at 5:12
-
\$\begingroup\$ Thanks for this, neat little trick for manipulating the result array \$\endgroup\$Daniel Dror– Daniel Dror2019年04月29日 12:25:19 +00:00Commented Apr 29, 2019 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– Veedrac2015年03月29日 19:17:54 +00:00Commented Mar 29, 2015 at 19:17