Python to Java converters?

Ype Kingma ykingma at
Fri Jan 18 15:21:59 EST 2002


you wrote:
> Hello,
> Since discovering Jython, most of my development has switched from Java to
> Python. However the core of the app remains Java for performance reasons.
> I can see that I will need to port some more code back to java for
> performance reasons, but I still find it worth my time to work in Python and
> port ... it's simply faster and so much fun :)

Join the club.

> But porting is boring. Before I start writing a tool to automate most of the
> work, is there anything out there that already does a reasonable conversion
> of Python to Java? Any other Jython users out there that think such a tool
> would be useful?

Before you start porting: have some tests (functional and performance) ready to
verify what you're doing; Jython and Java semantics are sometimes subtly different.

Let the jython profiler guide you. Learn to read through the effects of JIT
in the profile. (After say 50 times. the JIT takes some extra time and
from then on the time taken per call/iteration is a lot less.)
Try and optimize the python code first: choose the best algorithm,
use dicts instead of lists, try and reuse objects, avoid too long lists/strings,
etc. etc.
There are some good tips on the python web site that apply to jython as well.

After that it is unlikely that you'll gain more than a factor
of 10, except perhaps for special algorithms (eg. numerical), but there are
java libraries in many of these cases precisely for this reason.

In case there happens to be a java library available that does some of the work:
use this first. There are quite a few free java libs out there, and
a bit of searching is more interesting than debugging your own code.
You have the advantage over CPython that you can use these libs normally
unchanged. In abnormal circumstances you might have to add a bit of code
to make things visible from Jython.

That said, porting is boring indeed. What I did to minimize the work was to
create a java superclass for the jython class to be ported.
Then you need to port only the time critical methods, and move the variables
these methods use to the java superclass. Occasionally you'll need a PyObject
there, and some of the methods declared in (eg. __call__,
__str__ and __tojava__).
The non critical methods just stay on the compact (python) side, mostly

This way you can choose the methods and variables to be ported yourself, and
you might not need automatic porting tools at all.

> Thanks
> -Ed

My pleasure, have fun, boredom _can_ be avoided,

More information about the Python-list mailing list