|
From: Robert W. B. <rb...@di...> - 2001-06-01 08:26:44
|
Hi Stephen,
On Thu, 31 May 2001, Stephen Schleimer wrote:
> Folk,
>
> I am attempting to embed Jython in a system whose dynamic configuration
> is to include Jython expressions and code fragments. These code
> fragments are runtime evaluated and effect the progress of the overall
> system. The fragments serve as statement guards, iterators, etc.
> Thus, I need to do a good deal of expression evaluation, examine, in
> Java, the results of the evaluation and proceed. Not every expression
> will naturally set a variable in the environment storing its results.
> Thus, I would like to be able to determine the value of the evaluated
> expression without resorting to a storage side effect .
>
> The public interfaces to Py (such as runCode) return a value. However,
> in the way I have been experimenting with them, the value is always
> PyNone. In addition, a printed result of the expression evaluation is
> delivered to sysout.
>
> I am looking, therefore, for a couple of things:
>
> 1) reliably suppressing delivery of the computed result of the
> expression to sysout
> 2) reliably delivering the computed result to the invoker of the
> expression evaluation.
>
> Any aid is appreciated.
I'm probably misunderstanding this, but I think you could use the
PythonInterpreter instance to eval and exec things.
If the code fragments are statement/assignment-free, you can use
interp.eval(fragment) which returns a PyObject.
If statements/assignments exist in the fragments, you can use
interp.exec(someFunnyVar = fragment); then
PyObject x = PyObject.get("someFunnyVar").
Both ways `reliably' get values from the interpreter and neither prints to
stdout unless there is a print statement in the fragment.
Here's an example of eval'ing and exec'ing things and getting objects
from the interpreter in case it helps. I'm not sure I really understand
your situation, so complain lots if I'm way off base.
//====file: test.java
import java.util.Properties;
import org.python.core.*;
import org.python.util.*;
class test {
static PythonInterpreter interp;
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("python.home", "/home/rbill/jython-2.1a1");
PythonInterpreter.initialize(System.getProperties(), props, new String[0]);
interp = new PythonInterpreter();
// If you truly just need to evaluate, use this
PyObject i = interp.eval("1 and 0 or 2");
System.out.println( i );
// If you write eval'able fragments, return value are automatic.
// Here's another that processes this code file for lines
// containing "eval", plus does some other wacky stuff just to
// show how much can be done in an eval().
i = interp.eval("filter(lambda x: x.find('eval') > -1, [x.strip()[:-1] for x in " +
"open('test.java').readlines() if not x.lstrip().startswith('//')])");
System.out.println(i);
// If eval doesn't cut it,and you really need statements, you
// could assign the results of your fragments to a variable in the
// interpreter and extract it.
interp.exec("import sys; __frag_val = sys.path");
PyObject p = interp.get("__frag_val");
System.out.println(p);
}
}
cheers,
Robert
|