Creating a PyFunction

ProgDario dario at fga-software.com
Fri Sep 5 10:32:45 EDT 2003


dario at fga-software.com (ProgDario) wrote in message news:<67a857cc.0309050211.add7137 at posting.google.com>...
> 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.class);
		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()) + "]===========");
  }
}




More information about the Python-list mailing list