Java Code Examples for org.python.util.PythonInterpreter#set()

The following examples show how to use org.python.util.PythonInterpreter#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JythonServer.java    From floodlight_with_topoguard with Apache License 2.0 6 votes vote down vote up
/**
 * The main thread for this class invoked by Thread.run()
 *
 * @see java.lang.Thread#run()
 */
public void run() {
    PythonInterpreter p = new PythonInterpreter();
    for (String name : this.locals.keySet()) {
        p.set(name, this.locals.get(name));
    }

    URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
    String jarPath = jarUrl.getPath();
    if (jarUrl.getProtocol().equals("file")) {
        // If URL is of type file, assume that we are in dev env and set path to python dir.
        // else use the jar file as is
        jarPath = jarPath + "../../src/main/python/";
    }

    p.exec("import sys");
    p.exec("sys.path.append('" + jarPath + "')");
    p.exec("from debugserver import run_server");
    if (this.host == null) {
    	p.exec("run_server(port=" + this.port + ", locals=locals())");
    } else {
    	p.exec("run_server(port=" + this.port + ", host='" + this.host + "', locals=locals())");
    }
}
 
Example 2
Source File: InterpreterUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * 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__)");
}
 
Example 3
Source File: MenuController.java    From zest-writer with GNU General Public License v3.0 5 votes vote down vote up
public static String markdownToHtml(MdTextController index, String chaine) {
    PythonInterpreter console = index.getPyconsole();
    console.set("text", chaine);
    console.exec("render = mk_instance.convert(text)");
    PyString render = console.get("render", PyString.class);
    return render.toString();
}
 
Example 4
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
/**
 * 执行指定的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;
}
 
Example 5
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__();
	return pyobj;
}
 
Example 6
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function, PyObject params){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__(params.__getitem__(0));
	return pyobj;
}
 
Example 7
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
public void executeCommand(String cmd){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.exec(cmd); 
}
 
Example 8
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function, PyObject param0, PyObject param1){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__(param0, param1);
	return pyobj;
}
 
Example 9
Source File: PyInterpreterPool.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public void release(PythonInterpreter interpreter){
    // Clean the interpreter by setting the user defined instances to null
    PyObject locals = interpreter.getLocals();
    List<String> interpreterVars = new ArrayList<String>();
    for (PyObject item : locals.__iter__().asIterable()) {
        interpreterVars.add(item.toString());
    }
    for (String varName : interpreterVars) {
        if(varName.equals(NAME_VAR_STR) || varName.equals(DOC_VAR_STR)) continue;
        interpreter.set(varName, null);
    }
    pool.add(interpreter);
}