473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:

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
1 14570
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave Benjamin | last post by:
Hey good people, I've been doing a lot of simultaneous Jython and CPython programming lately, and just wanted to say, with no intended ill will toward any of the individuals who have been...
4
by: angel | last post by:
A java runtime environment includes jvm and java class (for example classes.zip in sun jre). Of course jython need jvm,but does it need java class. Thanx
7
by: Jan Gregor | last post by:
Hello I found that jython catches exact java exceptions, not their subclasses. Is there some way to get around this limitation (or error) ? My program has class representing database source...
5
by: Maurice Ling | last post by:
Hi, I have read that this had been asked before but there's no satisfactory replies then. I have a module (pA) written in python, which originally is called by another python module (pB), and...
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
8
by: Mark Fink | last post by:
I try to port a server application to Jython. At the moment I use Jython21\Lib\socket.py Currently I do face problems with casting the string "localhost" to the desired value:...
3
by: donkeyboy | last post by:
All, I'm having issues installing Jython on Windows XP. I've looked on the web and this newsgroup but to no avail. Any suggestions? The shell listing is below: NB I've got Cygwin installed,...
2
by: donkeyboy | last post by:
All, I've tried the jythonc compiler to try and create an applet to see how it works, but I get a number of Java compile errors that are way above my knowledge. Does anyone know what any of the...
5
by: sarup26 | last post by:
Hello .. I would like to know more about Python and Jython? What is the difference between both of them? What is the future for Jython and which are the areas where it is used? Swot
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.