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

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

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

PythonInterpreter.set介绍

[英]Sets a variable in the local namespace.
[中]在本地命名空间中设置变量。

代码示例

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

/**
 * Initializes the Jython interpreter and executes a python script.
 *
 * @param factory environment factory
 * @param scriptDirectory the directory containing all required user python scripts
 * @param scriptName the name of the main python script
 * @param args Command line arguments that will be delivered to the executed python script
 */
public static void initAndExecPythonScript(PythonEnvironmentFactory factory, java.nio.file.Path scriptDirectory, String scriptName, String[] args) {
  String[] fullArgs = new String[args.length + 1];
  fullArgs[0] = scriptDirectory.resolve(scriptName).toString();
  System.arraycopy(args, 0, fullArgs, 1, args.length);
  PythonInterpreter pythonInterpreter = initPythonInterpreter(fullArgs, scriptDirectory.toUri().getPath(), scriptName);
  pythonInterpreter.set("__flink_env_factory__", factory);
  pythonInterpreter.exec(scriptName + ".main(__flink_env_factory__)");
}

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

interpreter.set("document", document);
interpreter.set("root", document.getNodeLibrary().getRoot());
interpreter.set("parent", document.getActiveNetwork());
interpreter.set("node", document.getActiveNode());
interpreter.exec("from nodebox.node import *");
Exception pythonException = null;

代码示例来源: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: 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: 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: org.apache.pig/pig

static void setMain(boolean isMain) {
  if (isMain) {
    interpreter.set("__name__", "__main__");
  } else {
    interpreter.set("__name__", "__lib__");
  }
}

代码示例来源:origin: io.cloudslang/runtime-management-impl

private void prepareInterpreterContext(Map<String, Serializable> context) {
  for (Map.Entry<String, Serializable> entry : context.entrySet()) {
    interpreter.set(entry.getKey(), entry.getValue());
  }
}

代码示例来源:origin: CloudSlang/score

private void prepareInterpreterContext(Map<String, Serializable> context) {
  for (Map.Entry<String, Serializable> entry : context.entrySet()) {
    interpreter.set(entry.getKey(), entry.getValue());
  }
}

代码示例来源:origin: ch.epfl.bbp.nlp/bluima_jython

@Override
  public void process(JCas jcas) throws AnalysisEngineProcessException {
    try {
      interp.set("jcas", jcas); // make jcas available in script
      interp.execfile(scriptFileIs);
    } catch (Exception e) {
      throw new AnalysisEngineProcessException(
          AnalysisEngineProcessException.ANNOTATOR_EXCEPTION,
          new Object[] {}, e);
    }
  }
}

代码示例来源:origin: ch.epfl.bbp.nlp/bluima_jython

@Override
  public void process(JCas jcas) throws AnalysisEngineProcessException {
    try {
      interp.set("jcas", jcas); // make jcas available in script
      interp.exec(scriptString);
    } catch (Exception e) {
      throw new AnalysisEngineProcessException(
          AnalysisEngineProcessException.ANNOTATOR_EXCEPTION,
          new Object[] {}, e);
    }
  }
}

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

@Override
  public Object run(Map<String, Object> variables) {
    try (PythonInterpreter interp = new PythonInterpreter()) {
      if (variables != null) {
        for (Entry<String, Object> entry : variables.entrySet()) {
          interp.set(entry.getKey(), entry.getValue());
        }
      }
      return interp.eval(script);
    }
  }
}

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

PythonInterpreter interp = new PythonInterpreter();
 interp.set("a", this);
 interp.exec("import externalscript");
 interp.exec("externalscript.function(a)");

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

PythonInterpreter py = new PythonInterpreter();
String dataFolder,prepFolder;
py.execfile("filename.py");
py.set("df", dataFolder);
py.set("pf", prepFolder);
py.exec("prep = Preprocess(df, pf)");

//if the preprocess method does not return anything, you can do:
py.exec("prep.preprocess()");

//To get the return value in java, you can do:
SomeJavaClass retvalue = py.eval("prep.preprocess()").__tojava__(SomeJavaClass.class);

//To get and store the return type in the python local namespace:
py.exec("retValue = prep.preprocess()");

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

@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: 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: 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);
}

相关文章