Andrew Channels Dexter Pinion

Wherein I write some stuff that you may like to read. Or not, its up to you really.

September 29, 2005

Java or Python libraries in Jython?

I've been writing some more Jython code recently. In particular I've been quite enjoying the combination of both language's standard libraries. The ability to mix and match is saving me a lot of work.

Except where it doesn't of course. There is an inevitable overlap between the two standard libraries which leads me to the question - which should I use?

An example from my current code, I want to include the current date (in the format YYYY.MM.DD) in certain file names. I can get this using the Java standard library;

from java.util import Date
from java.text import SimpleDateFormat

df = SimpleDateFormat('yyyy.MM.dd')
today = df.format(Date())

Or I can use the Python library;

from time import strftime, localtime
today = strftime('%Y.%m.%d', localtime())

I suspect the Java version will be marginally faster as that's the language everything ends up running in, but the purist in me wants these scripts to be as close to pure Python code as possible.

Does anyone have an opinion on this?

Posted by Andy Todd at September 29, 2005 03:51 PM

Comments

Well, both ways seem equivalent effort coding-wise. If there isn't much speed difference either, I'd probably lean towards the viewing audience.

If mostly java folk will be following up maintenance of the script, the java libraries and familiar class names might make them less nervous with their script-language spelunking. ;)


Posted by: Chris on September 29, 2005 05:42 PM

import datetime
today = datetime.date.today().strftime('%Y.%m.%d')
# ;-)

Posted by: Marius Gedminas on September 29, 2005 07:20 PM

Marius. I'm afraid not. My code is in Jython which is Python 2.1. The datetime module wasn't introduced until Python 2.3

Posted by: Andy Todd on September 29, 2005 11:57 PM

I would think that you would want to go straight python (in case you ever wanted to run the code without a jvm)....

Posted by: matt on September 30, 2005 04:46 AM

I went through this about a year ago, and I found that using the Java equivalent was much better for some things like XML parsing. Also, the functions that I was controlling with Jython were Java functions, so using the Java instances of Java objects kept me from ever having to convert one to the other.

However, if I was writing something that wasn't controlling Java classes, I would probably tend toward using a pure Python approach. Keeping the python pure gives me the flexibility of using CPython or IronPython with the same code someday in the future.

-Jim

Posted by: jim on September 30, 2005 08:10 AM

"Does anybody an opinion?" Sure. Always.

First opinion: It really depends why you're writing the script in Jython in the first place. Will it be maintained by anybody other than yourself? Are they Java-heads or Python-heads?

Second opinion: On a purely programming level, I'd add that the Java standard library can be a PITA if you need to extract bits of the Date, such as month number or hour number.

Third opinion: consider using Groovy instead of Jython. (OK, maybe wait until early next year before considering Groovy for production code)

Posted by: Alan Green on September 30, 2005 09:12 AM