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

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

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

PythonInterpreter.<init>介绍

[英]Creates a new interpreter with an empty local namespace.
[中]创建一个本地命名空间为空的新解释器。

代码示例

代码示例来源:origin: org.freemarker/freemarker

private PythonInterpreter createInterpreter(Map vars) {
  PythonInterpreter pi = new PythonInterpreter();
  Iterator it = vars.entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry ent = (Map.Entry) it.next(); 
    pi.set((String) ent.getKey(), ent.getValue());
  }
  return pi;
}

代码示例来源:origin: apache/flink

private static synchronized PythonInterpreter initPythonInterpreter(String[] args, String pythonPath, String scriptName) {
    if (!jythonInitialized) {
      // the java stack traces within the jython runtime aren't useful for users
      System.getProperties().put("python.options.includeJavaStackInExceptions", "false");
      PySystemState.initialize(System.getProperties(), new Properties(), args);

      pythonInterpreter = new PythonInterpreter();

      pythonInterpreter.getSystemState().path.add(0, pythonPath);

      pythonInterpreter.setErr(System.err);
      pythonInterpreter.setOut(System.out);

      pythonInterpreter.exec("import " + scriptName);
      jythonInitialized = true;
    }
    return pythonInterpreter;
  }
}

代码示例来源: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: stackoverflow.com

System.getProperties(), new String[0]);  
this.interpreter = new PythonInterpreter();

代码示例来源:origin: nodebox/nodebox

public ImmutableMap<String, Function> call() throws Exception {
    // This creates a dependency between function and the client.
    // However, we need to know the load paths before we can do anything, so this is necessary.
    PythonUtils.initializePython();
    Py.getSystemState().path.append(new PyString(file.getParentFile().getCanonicalPath()));
    PythonInterpreter interpreter = new PythonInterpreter();
    try {
      interpreter.execfile(file.getCanonicalPath());
    } catch (IOException e) {
      throw new LoadException(file, e);
    } catch (PyException e) {
      throw new LoadException(file, e);
    }
    PyStringMap map = (PyStringMap) interpreter.getLocals();
    ImmutableMap.Builder<String, Function> builder = ImmutableMap.builder();
    for (Object key : map.keys()) {
      Object o = map.get(Py.java2py(key));
      if (o instanceof PyFunction) {
        String name = (String) key;
        Function f = new PythonFunction(name, (PyFunction) o);
        builder.put(name, f);
      }
    }
    return builder.build();
  }
});

代码示例来源:origin: nodebox/nodebox

add(consolePrompt, BorderLayout.SOUTH);
interpreter = new PythonInterpreter();
consolePrompt.requestFocus();
addFocusListener(this);

代码示例来源:origin: com.alexkasko.krakatau/krakatau-lib

/**
   * Constructor
   */
  public KrakatauLibrary() {
//        http://bugs.jython.org/issue1435
//        Options.showJavaExceptions = true;
//        Options.includeJavaStackInExceptions = true;
    this.python = new PythonInterpreter();
  }

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

import java.util.Properties;
import org.python.util.PythonInterpreter;

(...)

Properties properties = new Properties();
properties.setProperty("python.path", pythonPath);
PythonInterpreter.initialize(System.getProperties(), properties, new String[] { "" });
PythonInterpreter pythonInterpreter = new PythonInterpreter();
pythonInterpreter.exec("import codecs");
pythonInterpreter.exec("codecs.getreader('utf8')");

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

public ParsedScript(String source) {
  try (PythonInterpreter interp = new PythonInterpreter()) {
    script = interp.compile(source);
  }
}

代码示例来源:origin: org.carewebframework/org.carewebframework.web.script.jython

public ParsedScript(String source) {
  try (PythonInterpreter interp = new PythonInterpreter()) {
    script = interp.compile(source);
  }
}

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

Properties properties = System.getProperties();
properties.put("python.path", PATH_TO_PARENT_DIRECTORY_OF_AS1_PY);
PythonInterpreter.initialize(System.getProperties(), properties, new String[0]);
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("calen.py");

代码示例来源: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);
    }
  }
}

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

@Override
protected void setUp() throws Exception {
  interp = new PythonInterpreter(new PyStringMap(), new PySystemState());
  interp.exec("from java.io import Serializable");
  interp.exec("class Test(Serializable): pass");
  interp.exec("x = Test()");
}

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

@Override
protected void setUp() throws Exception {
  interp = new PythonInterpreter(new PyStringMap(), new PySystemState());
  a = new WrappedBoolean();
  b = new WrappedBoolean();
  a.setMutableValue(true);
  b.setMutableValue(false);
  interp.set("a", a);
  interp.set("b", b);
}

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

@Override
protected void setUp() throws Exception {
  interp = new PythonInterpreter(new PyStringMap(), new PySystemState());
  a = new WrappedInteger();
  b = new WrappedInteger();
  a.setMutableValue(13);
  b.setMutableValue(17);
  interp.set("a", a);
  interp.set("b", b);
}

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

@Override
protected void setUp() throws Exception {
  interp = new PythonInterpreter(new PyStringMap(), new PySystemState());
  a = new WrappedFloat();
  b = new WrappedFloat();
  a.setMutableValue(13.0);
  b.setMutableValue(17.0);
  interp.set("a", a);
  interp.set("b", b);
}

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

@Override
protected void setUp() throws Exception {
  interp = new PythonInterpreter(new PyStringMap(), new PySystemState());
  a = new WrappedLong();
  b = new WrappedLong();
  a.setMutableValue(13000000000L);
  b.setMutableValue(17000000000L);
  interp.set("a", a);
  interp.set("b", b);
}

相关文章