473,543 Members | 2,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

PythonInterpret er interp = new PythonInterpret er();
interp.execfile ('script1.py');
for (int i = 0; i < 1000; i++) {
interp.eval("ca lculate(8)");
}

Thanks in advance,

Dario
Jul 18 '05 #1
1 14593
da***@fga-software.com (ProgDario) wrote in message news:<67******* *************** ****@posting.go ogle.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:

PythonInterpret er interp = new PythonInterpret er();
interp.execfile ('script1.py');
for (int i = 0; i < 1000; i++) {
interp.eval("ca lculate(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 .PythonInterpre ter;
import org.python.core .*;

public class Calculate {
public static void main(String[] args){
PythonInterpret er interp = new PythonInterpret er();
interp.execfile ("script1.py ");
PyFunction func =
(PyFunction)int erp.get("calcul ate",PyFunction .class);
SimpleDateForma t sdf = new SimpleDateForma t("dd/MM/yyyy hh.mm.ss");
System.out.prin tln("======[" + sdf.format(new Date()) +
"]===========");
for (int i=1 ; i<10000 ; ++i) {
// Assuming calculate takes a float argument.
func.__call__(n ew PyFloat(i));
//interp.eval("ca lculate(" + i + ")");
}
System.out.prin tln("======[" + 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
2102
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 generous enough to make the two languages possible, that, well, they're kinda different. I guess it was inevitable, but with Jython stuck at Python 2.1,...
4
3411
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
12737
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 and specialed classes for particulars databases. Now there are two options - to include exception (subclasses of SQLException) for every db in except...
5
3671
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 passes a python object (pO) to pB. Now I require pA to be called in a java class (jC) and pass pO into jC. As pA uses non-python modules, I am not...
12
5894
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 says: usage: java fit.FitServer host port socketTicket -v verbose I think this is because I do not understand the jython mechanism for...
8
5984
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: D:\AUT_TEST\workspace\JyFIT>jython fit/JyFitServer2.py localhost 1234 23 localhost Traceback (innermost last): File "fit/JyFitServer2.py", line 146, in ?
3
4136
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, hence the Unix 'ls' on a Windows box C:\>cd Jython
2
2459
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 following means? I'm using JDK 1.5.0_09, under Win XP SP2. Runnnig the file " jython FILENAME" works, so I don't know what's happening. Any...
5
3596
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
7401
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7681
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5260
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4886
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3387
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
958
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
628
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.