Wednesday, March 2, 2011

jython + Google Guava's base.CaseFormat

I was trying to research Google Collections for use in jython and stumbled upon Solid Craft's blog and his description of Guava base's CaseFormat utility.  Where I work we're a Java shop, and use of camelCase infiltrates all code, even jython code.  CaseFormat provides functionality for changing these variable names to a number of different formats.

$ /usr/local/jdk-1.7.0/bin/java -jar jython.jar
Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54)
[OpenJDK Client VM (Sun Microsystems Inc.)] on java1.7.0-internal
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> # get downloaded jar into sys.path
>>> sys.path.append('/home/carl/Downloads/guava-r08/guava-r08.jar')
>>> from com.google.common.base import CaseFormat as cf
>>> dir(cf)
['LOWER_CAMEL', 'LOWER_HYPHEN', 'LOWER_UNDERSCORE', 'UPPER_CAMEL', . . .]
>>> varname = 'someVariable'
>>> cf.LOWER_CAMEL.to(cf.LOWER_UNDERSCORE, varname)
u'some_variable'
>>> cf.LOWER_CAMEL.to(cf.UPPER_UNDERSCORE, varname)
u'SOME_VARIABLE'
>>> cf.LOWER_CAMEL.to(cf.UPPER_CAMEL, varname)
u'SomeVariable'
>>>



For cycling through a file full of camel case or underscored variable names, even by hand in the interpreter, this could save some time and typing.
 


No comments:

Post a Comment

Post a Comment