org.python.util.PythonInterpreter.get()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(259)

本文整理了Java中org.python.util.PythonInterpreter.get()方法的一些代码示例,展示了PythonInterpreter.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。PythonInterpreter.get()方法的具体详情如下:
包路径:org.python.util.PythonInterpreter
类名称:PythonInterpreter
方法名:get

PythonInterpreter.get介绍

[英]Returns the value of a variable in the local namespace.
[中]返回本地命名空间中变量的值。

代码示例

代码示例来源:origin: Raysmond/SpringBlog

@Override
  public String highlight(String content) {
    PythonInterpreter interpreter = new PythonInterpreter();

    // Set a variable with the content you want to work with
    interpreter.set("code", content);

    // Simple use Pygments as you would in Python
    interpreter.exec("from pygments import highlight\n"
        + "from pygments.lexers import PythonLexer\n"
        + "from pygments.formatters import HtmlFormatter\n"
        + "\nresult = highlight(code, PythonLexer(), HtmlFormatter())");

    return interpreter.get("result", String.class);
  }
}

代码示例来源:origin: spring-projects/spring-security

public boolean before(Authentication authentication, MethodInvocation mi,
    PreInvocationAttribute preAttr) {
  PythonInterpreterPreInvocationAttribute pythonAttr = (PythonInterpreterPreInvocationAttribute) preAttr;
  String script = pythonAttr.getScript();
  PythonInterpreter python = new PythonInterpreter();
  python.set("authentication", authentication);
  python.set("args", createArgumentMap(mi));
  python.set("method", mi.getMethod().getName());
  Resource scriptResource = new PathMatchingResourcePatternResolver()
      .getResource(script);
  try {
    python.execfile(scriptResource.getInputStream());
  }
  catch (IOException e) {
    throw new IllegalArgumentException("Couldn't run python script, " + script, e);
  }
  PyObject allowed = python.get("allow");
  if (allowed == null) {
    throw new IllegalStateException("Python script did not set the permit flag");
  }
  return (Boolean) Py.tojava(allowed, Boolean.class);
}

代码示例来源:origin: stackoverflow.com

PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import sys\nsys.path.append('pathToModiles if they're not there by default')\nimport yourModule");
// execute a function that takes a string and returns a string
PyObject someFunc = interpreter.get("funcName");
PyObject result = someFunc.__call__(new PyString("Test!"));
String realResult = (String) result.__tojava__(String.class);

代码示例来源:origin: org.zkoss.zk/zkmax

protected Object get(String name) {
  //Bug 2208873: Don't use _ip.get(String, Object) since it
  //doesn't handle null well
  PyObject val = _ip.get(name);
  return val != null ? Py.tojava(val, Object.class): null;
}

代码示例来源:origin: org.apache.giraph/giraph-core

/**
 * Instantiate new instance of the Jython class
 *
 * @param className Jython class name
 * @return new instance of class
 */
public static PyObject newInstance(String className) {
 PyObject pyClass = JythonUtils.getInterpreter().get(className);
 PyObject pyObject = pyClass.__call__();
 return pyObject;
}

代码示例来源:origin: org.apache.pig/pig

/**
 * Gets the Python function object.
 * @param path Path of the jython script file containing the function.
 * @param functionName Name of the function
 * @return a function object
 * @throws IOException
 */
public static PyFunction getFunction(String path, String functionName) throws IOException {
  Interpreter.setMain(false);
  Interpreter.init(path, null);
  return (PyFunction) Interpreter.interpreter.get(functionName);
}

代码示例来源:origin: NGDATA/lilyproject

public JythonLineMapper(String pythonCode, String recordMapperSymbol, Writer stderrWriter) {
  interpreter = new PythonInterpreter();
  interpreter.setErr(stderrWriter);
  // TODO Should we (or can we) restrict what can be done here?
  interpreter.exec(pythonCode);
  mapperCallable = interpreter.get(recordMapperSymbol);
  if (mapperCallable == null) {
    throw new IllegalArgumentException("Symbol " + recordMapperSymbol + " cannot be found");
  }
}

代码示例来源:origin: org.python/jython

@Override
  public void run() {
    PythonInterpreter interp = new PythonInterpreter();
    interp.exec("import sys");
    interp.set("a", new PyInteger(41));
    int set = Py.tojava(interp.get("a"), Integer.class);
    assertEquals(41, set);
    interp.exec("x = 'hello ' + 'goodbye'");
    assertEquals("hello goodbye", Py.tojava(interp.get("x"), String.class));
    doneSignal.countDown();
  }
}.start();

代码示例来源:origin: org.python/jython

public void testMod() {
    interp.exec("c = b % a");
    assertEquals(new PyLong(4000000000L), interp.get("c"));
  }
}

代码示例来源:origin: org.python/jython

/**
 * Test importing the _io module into the global namespace of {@link #interp}.
 */
@Test
public void moduleImport() {
  interp.exec("import _io");
  PyObject _io = interp.get("_io");
  org.junit.Assert.assertNotNull(_io);
}

代码示例来源:origin: com.sangupta/pepmint

/**
 * Obtain a new lexer implementation based on the given lexer name.
 * 
 * @param name
 * @return
 */
public Lexer newLexer(String name) {
  PyObject object = pythonInterpreter.get("get_lexer_by_name");
  object = object.__call__(new PyString(name));
  return new Lexer(object);
}

代码示例来源:origin: org.python/jython

public void testMod() {
    interp.exec("c = b % a");
    assertEquals(new PyFloat(4), interp.get("c"));
  }
}

代码示例来源:origin: org.python/jython

public void testOr() {
    interp.exec("c = a or b");
    assertEquals(new PyBoolean(true), interp.get("c"));
    a.setMutableValue(false);
    interp.exec("c = a or b");
    assertEquals(new PyBoolean(false), interp.get("c"));
  }
}

代码示例来源:origin: org.python/jython

public void testAnd() {
  interp.exec("c = a and b");
  assertEquals(new PyBoolean(false), interp.get("c"));
  b.setMutableValue(true);
  interp.exec("c = a and b");
  assertEquals(new PyBoolean(true), interp.get("c"));
}

代码示例来源:origin: org.python/jython

public void testAdd() {
  interp.exec("c = a + b");
  assertEquals(new PyFloat(30), interp.get("c"));
  b.setMutableValue(18.0);
  interp.exec("c = a + b");
  assertEquals(new PyFloat(31), interp.get("c"));
}

代码示例来源:origin: org.python/jython

public void testAdd() {
  interp.exec("c = a + b");
  assertEquals(new PyInteger(30), interp.get("c"));
  b.setMutableValue(18);
  interp.exec("c = a + b");
  assertEquals(new PyInteger(31), interp.get("c"));
}

代码示例来源:origin: org.python/jython

public void testAdd() {
  interp.exec("c = a + b");
  assertEquals(new PyLong(30000000000L), interp.get("c"));
  b.setMutableValue(18000000000L);
  interp.exec("c = a + b");
  assertEquals(new PyLong(31000000000L), interp.get("c"));
}

代码示例来源:origin: stackoverflow.com

PythonInterpreter jython = new PythonInterpreter();
jython.set("out", new PyString());
jython.exec("out = ''");
jython.exec("out += 'Test1\\n'");
jython.exec("out += 'Test2\\n'");
System.out.println(jython.get("out").toString());

代码示例来源:origin: stackoverflow.com

PythonInterpreter interpreter = new PythonInterpreter();
 // Append directory containing module to python search path and import it
 interpreter.exec("import sys\n" + "sys.path.append(pathToModule)\n" + 
 "from bar import foo");
 PyObject meth = interpreter.get("foo");
 PyObject result = meth.__call__(new PyString("Test!"));
 String real_result = (String) result.__tojava__(String.class);

代码示例来源:origin: org.picocontainer.script/picocontainer-script-jython

protected PicoContainer createContainerFromScript(PicoContainer parentContainer, Object assemblyScope) {
    try {
      PythonInterpreter interpreter = new PythonInterpreter();
      interpreter.set("parent", parentContainer);
      interpreter.set("assemblyScope", assemblyScope);
      interpreter.execfile(getScriptInputStream(), "picocontainer.py");
      return (PicoContainer) interpreter.get("pico", PicoContainer.class);
    } catch (IOException e) {
      throw new ScriptedPicoContainerMarkupException(e);
    }
  }
}

相关文章