Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to generate java with python?

Whats the best way to generate java from python?

I want to write a decorator that generates java code to call a json version of a function (I can use existing decorators to export the json api).

Whats the best way to generate the java, should I consider stuff like FSMs here?

Ideally I can write my code once, for the server and generate code to interface with it for various languages (java first).

Edit (pulled from a comment on a deleted answer):
The java code will be running on android, while the python code will be in a django server... Also, I want to be able to statically generate the java code, and have this as part of an API that people can use.

like image 488
Stuart Axon Avatar asked Nov 06 '22 05:11

Stuart Axon


2 Answers

You can always create java code the same way webapps create HTML: with a template. Then you can have the java source code compiled into bytecode using a regular Java compiler ( see: Package javax.tools )

Not necessarily the best option, but definitely an option ( and quite simple btw )

like image 80
OscarRyz Avatar answered Nov 11 '22 04:11

OscarRyz


Reuse is good. Complexity is a price.

It will make you pay twice or more later on. Measure your ROI (Return on Investment) before you make a leap.

[Edit:]

  1. You could use Jython which will make it easy to write glue code as calling Java functions.
  2. jythonc transforms Python source code into Java source code then invokes a Java compiler to turn it into .class files.
  3. As said, you could use templating. Some one did use cheetah for that purpose. Though the link is not available any more.
like image 29
pyfunc Avatar answered Nov 11 '22 04:11

pyfunc