Creating a PyFunction

P

ProgDario

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
 
P

ProgDario

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()) + "]===========");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,102
Messages
2,570,646
Members
47,247
Latest member
GabrieleL2

Latest Threads

Top