By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,526 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,526 IT Pros & Developers. It's quick & easy.

Executing a Jython function from Java

P: n/a
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:

PythonInterpreter interp = new PythonInterpreter();
interp.execfile('script1.py');
for (int i = 0; i < 1000; i++) {
interp.eval("calculate(8)");
}

Thanks in advance,

Dario
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
da***@fga-software.com (ProgDario) wrote in message news:<67**************************@posting.google. com>...
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:

PythonInterpreter interp = new PythonInterpreter();
interp.execfile('script1.py');
for (int i = 0; i < 1000; i++) {
interp.eval("calculate(8)");
}

Thanks in advance,

Dario


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.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()) +
"]===========");
}
}
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.