Executing a Jython function from Java
Collapse
This topic is closed.
X
X
Collapse
-
ProgDarioProgDario
Executing a Jython function from Java
Hello,
in the site I found a lot of complicated examples, but I can't find
the simple one I'm looking for.
The problem is:
I have 1 jython file (script1.py) with a function named 'calculate',
and a java file (run.java).
I'd like to call the jython function from the java class recursively.
<<====<<
I think it can be possible to get a PyFunction object that rapresents
the function, and then call the function from the already evaluated
object, so I don't need to re-evaluate it every loop.
My code is something like this:
PythonInterpret er interp = new PythonInterpret er();
interp.execfile ('script1.py');
for (int i = 0; i < 1000; i++) {
interp.eval("ca lculate(8)");
}
Thanks in advance,
Dario
-
ProgDarioProgDario
Re: Executing a Jython function from Java
dario@fga-software.com (ProgDario) wrote in message news:<67a857cc. 0309050122.644e a7ee@posting.go ogle.com>...[color=blue]
> Hello,
>
> in the site I found a lot of complicated examples, but I can't find
> the simple one I'm looking for.
>
> The problem is:
>
> I have 1 jython file (script1.py) with a function named 'calculate',
> and a java file (run.java).
>
> I'd like to call the jython function from the java class recursively.
> <<====<<
>
> I think it can be possible to get a PyFunction object that rapresents
> the function, and then call the function from the already evaluated
> object, so I don't need to re-evaluate it every loop.
>
> My code is something like this:
>
> PythonInterpret er interp = new PythonInterpret er();
> interp.execfile ('script1.py');
> for (int i = 0; i < 1000; i++) {
> interp.eval("ca lculate(8)");
> }
>
> Thanks in advance,
>
> Dario[/color]
I searched the web for a long time, and finally Jeff Emanuel emailed
me and solved the problem.
That's the code, any advice is apreciated:
import java.io.*;
import java.util.*;
import java.text.*;
import org.python.util .PythonInterpre ter;
import org.python.core .*;
public class Calculate {
public static void main(String[] args){
PythonInterpret er interp = new PythonInterpret er();
interp.execfile ("script1.py ");
PyFunction func =
(PyFunction)int erp.get("calcul ate",PyFunction .class);
SimpleDateForma t sdf = new SimpleDateForma t("dd/MM/yyyy hh.mm.ss");
System.out.prin tln("======[" + sdf.format(new Date()) +
"]===========");
for (int i=1 ; i<10000 ; ++i) {
// Assuming calculate takes a float argument.
func.__call__(n ew PyFloat(i));
//interp.eval("ca lculate(" + i + ")");
}
System.out.prin tln("======[" + sdf.format(new Date()) +
"]===========");
}
}
Comment