Creating a PyFunction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ProgDario

    Creating a PyFunction

    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
  • ProgDario

    #2
    Re: Creating a PyFunction

    [email protected] (ProgDario) wrote in message news:<67a857cc. 0309050211.add7 [email protected] gle.com>...[color=blue]
    > 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[/color]


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

    Comment

    Working...