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

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

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

PythonInterpreter.exec介绍

[英]Executes a string of Python source in the local namespace.
[中]在本地命名空间中执行一个Python源字符串。

代码示例

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

public void execute(String script, Map vars) throws BuildException {
  PythonInterpreter pi = createInterpreter(vars);
  pi.exec(script);
}

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

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

代码示例来源:origin: ggp-org/ggp-base

public static void main(String[] args) {
    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.exec("from scripts import *");
    interpreter.exec("import external.JythonConsole.console");
    interpreter.exec("external.JythonConsole.console.main(locals())");
  }
}

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

import org.python.util.PythonInterpreter;  
public class PythonScript{  
  public static void main(String args[]){  
    PythonInterpreter interpreter = new PythonInterpreter();  
    interpreter.exec("days=('One','Two','Three','Four'); ");  
    interpreter.exec("print days[1];");    
  }
}

代码示例来源:origin: MarkusBernhardt/robotframework-selenium2library-java

@Override
  protected PythonInterpreter initialValue() {
    PythonInterpreter pythonInterpreter = new PythonInterpreter();
    pythonInterpreter
        .exec("from robot.libraries.BuiltIn import BuiltIn; from robot.running.context import EXECUTION_CONTEXTS; from robot.api import logger;");
    return pythonInterpreter;
  }
};

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

import org.python.util.PythonInterpreter;

public class JythonTest {
  public static void main(String[] args) {
    PythonInterpreter interp = new PythonInterpreter();
    interp.exec("if 2 > 1: print 'in if statement!'");
  }
}

代码示例来源: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());
  interp.exec("from java.io import Serializable");
  interp.exec("class Test(Serializable): pass");
  interp.exec("x = Test()");
}

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

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: 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.jvnet.hudson.plugins/jython

public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)  throws IOException, InterruptedException {
    PySystemState sys = new PySystemState();
    sys.setCurrentWorkingDir(build.getWorkspace().getRemote());
    PythonInterpreter interp = new PythonInterpreter(null, sys);

    interp.setOut(listener.getLogger());
    interp.setErr(listener.getLogger());
    interp.exec(this.getCommand());
    interp.cleanup();

    build.setResult(Result.SUCCESS);
    return true;
  }
}

相关文章