Hi.
>
> How do I run a piece of frozen python script within an embedded jython
> interpreter?
First jythonc and the interpeter are not incompatible but do not
collabarate much. See:
http://aspn.activestate.com/ASPN/Mail/Message/555125
In your case is not a problem because you want construct
your Python/Java object on Java side.
> i.e. How do I get the following contrived example to work?
>
> Foo.py:
> class Foo:
> def __init__(self, blah):
> self.blah = blah
> def doSomething():
> blah.doSomethingElse()
>
> Foo.py is precompiled to Foo.class using jythonc,
This produces a java class that correspond to a Python module
not to a class, the java class has just basically a main method.
If you want a Java (proxy) class corresponding to a python class,
your python class should inherit from Java:
import java
class Foo(java.lang.Object):
def __init__(self, blah):
"@sig public Foo(String blah)"
self.blah = blah
def doSomething():
blah.doSomethingElse()
The @sig doc strings are a feature only of jythonc, they make a
method java visible (the class will have a concrete java method
with that signature) otherwise unless overriding a method is just a
pure Python method not directly accessible from Java side.
So in in this case doSomething will remain a pure Python method.
>
> PyObject f = new Foo(); // how do I call __init__ with arguments?
Now something like this
Foo f = new Foo("blah");
should work.
Notice that java side Foo class does not inherit form PyObject
in this case.
>
> PythonInterpreter interp = new PythonInterpreter();
> interp.set("foo", f);
> interp.exec("foo.doSomething()");
This works because the java/python conversion behind the scene
accesssz the full-fledged python instance, for which the java class
Foo is just a proxy :) .
>
> Alternatively, how would I bytecompile a script, and pass the code object
> to the interpreter? I tried to use jythonc to create a class file, and
> then used that as a source for an InputStream to
> PythonInterpreter.exexfile(),
This doesn't work.
> but that just gave me syntax errors on the
> binary data at runtime. I guess it needs to be a python byte compiled
> file instead...?
No jython does not never deal with python bytecode.
> bar.py:
> print "hello world"
> print "bye world"
>
> How do I bytecompile bar.py?
> how do I then load and run the bytecompiled bar.py using a
> PythonInterpreter instance?
>
*warning* hack:
org.python.core.PyRunnable bar = new bar._PyInner();
PyObject code = bar.getMain();
interp.exec(code)
or some flavor of what was in the posting cited
above.
regards.
|