當前位置: 首頁>>代碼示例>>Java>>正文


Java PythonInterpreter.eval方法代碼示例

本文整理匯總了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));
}
 
開發者ID:golo-lang,項目名稱:golo-jmh-benchmarks,代碼行數:21,代碼來源:CodeLoaderTest.java

示例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);
}
 
開發者ID:soulmachine,項目名稱:JythonExample,代碼行數:35,代碼來源:JythonExample.java

示例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;
}
 
開發者ID:hoozheng,項目名稱:AndroidRobot,代碼行數:15,代碼來源:AndroidDriver.java

示例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;
	}

}
 
開發者ID:therelaxist,項目名稱:spring-usc,代碼行數:14,代碼來源:MiniSelection.java


注:本文中的org.python.util.PythonInterpreter.eval方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。