da***@fga-software.com (ProgDario) wrote in message news:<67*************************@posting.google.c om>...
Hello,
I have a simple question I can't find on the FAQ.
I want to instantiate a PyFunction from a Java class, but the function
is on a jython file (script1.py).
How can I get a PyFunction object referencing the function in the
external file?
Then, how can I call the function (__exec__ method ?) passing in an
argument?
Thanks in advance
Dario
I found the solution, it's been emailed me from Jeff Emanuel
The complete code follows:
import java.io.*;
import java.util.*;
import java.text.*;
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class Calculate {
public static void main(String[] args){
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("script1.py");
PyFunction func = (PyFunction)interp.get("calculate",PyFunction.clas s);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh.mm.ss");
System.out.println("======[" + sdf.format(new Date()) + "]===========");
for (int i=1 ; i<10000 ; ++i) {
// Assuming calculate takes a float argument.
func.__call__(new PyFloat(i));
//interp.eval("calculate(" + i + ")");
}
System.out.println("======[" + sdf.format(new Date()) + "]===========");
}
}