本文整理汇总了Java中org.python.util.PythonInterpreter.eval方法的典型用法代码示例。如果您正苦于以下问题:Java PythonInterpreter.eval方法的具体用法?Java PythonInterpreter.eval怎么用?Java PythonInterpreter.eval使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.python.util.PythonInterpreter
的用法示例。
在下文中一共展示了PythonInterpreter.eval方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_jython_loading
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
@Test
public void test_jython_loading() throws Throwable {
CodeLoader loader = new CodeLoader();
PythonInterpreter interpreter = loader.jython("check");
PyObject object = interpreter.get("truth");
assertTrue(object instanceof PyFunction);
PyFunction fun = (PyFunction) object;
assertEquals(new PyInteger(42), fun.__call__());
object = interpreter.get("incr");
assertTrue(object instanceof PyFunction);
fun = (PyFunction) object;
assertEquals(new PyInteger(3), fun.__call__(new PyInteger(2)));
fun = (PyFunction) interpreter.get("foo");
interpreter.exec("from org.gololang.microbenchmarks.support import Foo");
PyObject foo = interpreter.eval("Foo()");
assertEquals(new PyString("foo"), fun.__call__(foo));
}
示例2: main
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
/**
* Main entrypoint.
*
* @param args arguments
* @throws ScriptException ScriptException
*/
public static void main(final String[] args) throws ScriptException {
listEngines();
// Integrate with ScriptEngine
{
final ScriptEngineManager mgr = new ScriptEngineManager();
final ScriptEngine pyEngine = mgr.getEngineByName("python");
try {
pyEngine.eval("print \"Python - Hello, world!\"");
} catch (ScriptException ex) {
ex.printStackTrace();
}
}
final PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("print \"Python - Hello, world!\"");
PyObject result = interpreter.eval("2 + 3");
System.out.println(result.toString());
// Define a function and call it from Java
interpreter.exec("def myLowerCase(s):\n\treturn s.lower()\n");
final PyObject myLowerCase = interpreter.get("myLowerCase");
result = myLowerCase.__call__(new PyString("TEST!"));
final String realResult = (String)result.__tojava__(String.class);
System.out.println(realResult);
}
示例3: executePython
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
/**
* 执行指定的python文件
* @param filePath
* @return
*/
private PyObject executePython(String filePath){
PythonInterpreter interpreter = new PythonInterpreter();
Vector<AndroidDriver> drivers = new Vector();
drivers.add(this);
interpreter.set("device", drivers);
interpreter.execfile(filePath);
PyObject ret = interpreter.eval("True");
return ret;
}
示例4: evaluatePythonExpression
import org.python.util.PythonInterpreter; //导入方法依赖的package包/类
private boolean evaluatePythonExpression(Row r, PyCode code, PythonInterpreter interpreter) {
evalColumns.clear();
try {
ArrayList<Node> nodes = new ArrayList<Node>(r.getNodes());
Node node = nodes.get(0);
interpreter.getLocals().__setitem__("nodeid", new PyString(node.getId()));
PyObject output = interpreter.eval(code);
return PythonTransformationHelper.getPyObjectValueAsBoolean(output);
}catch(Exception e) {
return onError;
}
}