Porting a library from Java to Python

I'm going to migrate a small library from Java to Python and would like some tips (small ones - a few thousand lines of code). I studied Java code a bit and noticed some design patterns that are common to both languages. However, there were definitely idioms of Java unity (singletones, etc.), which are usually not well understood in the Python world.

I know that there is at least one tool (j2py) that will turn a .java file into a .py file by passing AST. Some initial experiments gave less favorable results.

Should I even think about using an automated tool to create some code, or are the languages ​​so different that any tool could create enough processing to justify writing from scratch?

If the tools are not the devil, are there other than j2py that can at least handle the import management of the same project? I do not expect any tool to match third-party libraries from one language to another in another.

+6
java python
source share
7 answers

If it were me, I would have thought about doing the work manually. A few thousand lines of code is not a lot of code, and by rewriting it yourself (instead of automatically translating it), you can decide how to use the Python idioms correctly. (FWIW, I have been running Java almost exclusively for 9 years, and now I work in Python, so I know what kind of translation you will need to do.)

+9
source share

The code is always better the second time you write it ... Plus, several thousand lines of Java can probably be translated into several hundred Python.

+6
source share

Take a look at Jython . It can quite easily integrate Python on top of Java and provide access to Java libraries, but still allow you to act on them dynamically.

+5
source share

Automatic translators (f2c, j2py, whatever) usually emit code that you would not like to touch manually. This is normal when all you have to do is use the output (for example, if you have a C compiler and a Fortran compiler, f2c allows you to compile Fortran programs), but it's awful when you need to do something with the code after that. If you intend to use it as anything other than a black box, translate it manually. With this size, it will not be too difficult.

+3
source share

I would write it again manually. I don’t know any automatic tools that would create a not-so-disgusting looking Python, and I ported Java code to Python myself, I found that the result was both better and original and much shorter.

You get quality because Python is more expressive (for example, an anonymous inner class of MouseAdapters, etc., goes in the direction of simple functions of the first class), and you also get the opportunity to write it a second time.

It is also much shorter: for example, 99% of getters / setters can simply be excluded due to direct access to the fields. For the remaining 1% that really do something, you can use property() .

However, as David said, if you don’t need to read or maintain the code, the automatic translator will be fine.

+3
source share

Jython is not what you are looking for in the final solution, but it will make the porting much smoother.

My approach:

  • If existing tests exist (unit or otherwise), rewrite them in Jython (using Python unittest)
  • Write some performance tests in Jython (tests that record current behavior)
  • Run the porting class by class:
    • For each class, it is a subclass in Jython and it portes the methods one by one, making the method in an abstract superclass
    • After each change, run the tests !
  • You will now have a working Jython code, which we hope has minimal Java dependencies.
  • Run the tests in CPython and fix whatever is left.
  • Refactor - you'll want Pythonify code, maybe simplify it with Python idioms. It is safe and easy due to tests.

I have had this in the past with great success.

+2
source share

I used Java2Python. This is not so bad, you still need to understand the code, because it does not do everything right, but it really helps.

0
source share

All Articles