Java Code Examples for org.python.util.PythonInterpreter#get()
The following examples show how to use
org.python.util.PythonInterpreter#get() .
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: FrmMain.java From MeteoInfo with GNU Lesser General Public License v3.0 | 6 votes |
/** * Load an application * * @param plugin Application */ public void loadApplication(Application plugin) { this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); try { PythonInterpreter interp = this.getConsoleDockable().getInterpreter(); String path = plugin.getPath(); interp.exec("import " + path); interp.exec("from " + path + ".loadApp import LoadApp"); PyObject loadClass = interp.get("LoadApp"); PyObject loadObj = loadClass.__call__(); IPlugin instance = (IPlugin) loadObj.__tojava__(IPlugin.class); instance.setApplication(FrmMain.this); instance.setName(plugin.getName()); plugin.setPluginObject(instance); plugin.setLoad(true); instance.load(); } catch (Exception e) { e.printStackTrace(); } this.setCursor(Cursor.getDefaultCursor()); }
Example 2
Source File: XunfengInner.java From TrackRay with GNU General Public License v3.0 | 6 votes |
@Override public void process() { try { PythonInterpreter interpreter = python.interpreter(); interpreter.execfile(filename); PyFunction check = interpreter.get("check", PyFunction.class); PyObject check_call = check.__call__(new PyString(ip), new PyInteger(port), new PyInteger(timeout)); String result = check_call.toString(); if (result!=null && !StringUtils.contains(result,"None") && !StringUtils.contains(result,"False")) { //PyObject get_plugin_info = interpreter.get("get_plugin_info").__call__(); //Map map = (Map) get_plugin_info.getDict().__tojava__(Map.class); this.result = result; return; } }catch (Exception e){ log.error(e.toString()); } result=""; }
Example 3
Source File: JIntrospect.java From MeteoInfo with GNU Lesser General Public License v3.0 | 5 votes |
/** * Return list of token for command. * @param command The command * @return Token list * @throws java.io.UnsupportedEncodingException */ public PyList getTokens(String command) throws UnsupportedEncodingException { StringBuilder sb = new StringBuilder(); sb.append("import cStringIO"); sb.append("\n"); sb.append("import tokenize"); sb.append("\n"); sb.append("command = str('"); sb.append(command); sb.append("')"); sb.append("\n"); sb.append("f = cStringIO.StringIO(command)"); sb.append("\n"); sb.append("tokens = []"); sb.append("\n"); sb.append("def eater(*args):"); sb.append("\n"); sb.append(" tokens.append(args)"); sb.append("\n"); sb.append("tokenize.tokenize_loop(f.readline, eater)"); sb.append("\n"); String code = sb.toString(); String encoding = "utf-8"; PythonInterpreter pi = new PythonInterpreter(); try { pi.execfile(new ByteArrayInputStream(code.getBytes(encoding))); PyList tokens = (PyList)pi.get("tokens"); return tokens; } catch (Exception e){ return null; } }
Example 4
Source File: JIntrospect.java From MeteoInfo with GNU Lesser General Public License v3.0 | 5 votes |
/** * Return list of token for command. * @param command The command * @return Token list * @throws java.io.UnsupportedEncodingException */ public PyList getTokens(String command) throws UnsupportedEncodingException { StringBuilder sb = new StringBuilder(); sb.append("import cStringIO"); sb.append("\n"); sb.append("import tokenize"); sb.append("\n"); sb.append("command = str('"); sb.append(command); sb.append("')"); sb.append("\n"); sb.append("f = cStringIO.StringIO(command)"); sb.append("\n"); sb.append("tokens = []"); sb.append("\n"); sb.append("def eater(*args):"); sb.append("\n"); sb.append(" tokens.append(args)"); sb.append("\n"); sb.append("tokenize.tokenize_loop(f.readline, eater)"); sb.append("\n"); String code = sb.toString(); String encoding = "utf-8"; PythonInterpreter pi = new PythonInterpreter(); try { pi.execfile(new ByteArrayInputStream(code.getBytes(encoding))); PyList tokens = (PyList)pi.get("tokens"); return tokens; } catch (Exception e){ return null; } }
Example 5
Source File: FrmAppsManager.java From MeteoInfo with GNU Lesser General Public License v3.0 | 5 votes |
public Application readPyApp(String path, String fileName) { try { Application plugin = new Application(); plugin.setPath(path); plugin.setClassName("LoadApp"); PythonInterpreter interp = this.parent.getConsoleDockable().getInterpreter(); interp.exec("import " + path); interp.exec("from " + path + ".loadApp import LoadApp"); PyObject loadClass = interp.get("LoadApp"); PyObject loadObj = loadClass.__call__(); IPlugin instance = (IPlugin) loadObj.__tojava__(IPlugin.class); plugin.setPluginObject(instance); return plugin; } catch (Exception e) { e.printStackTrace(); } return null; }
Example 6
Source File: MenuController.java From zest-writer with GNU General Public License v3.0 | 5 votes |
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 7
Source File: AndroidDriver.java From AndroidRobot with Apache License 2.0 | 5 votes |
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 8
Source File: AndroidDriver.java From AndroidRobot with Apache License 2.0 | 5 votes |
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 9
Source File: AndroidDriver.java From AndroidRobot with Apache License 2.0 | 5 votes |
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 10
Source File: PyRoutineWrapper.java From spliceengine with GNU Affero General Public License v3.0 | 4 votes |
/** * A wrapper static method for Python Stored Procedure * * @param args if the registered stored procedure has n parameters, the args[0] to args[n-1] are these parameters. * The args[n] must be a String which contains the Python script. The rest of elements in the args are ResultSet[]. * In the current test version only allows one ResultSet[] */ public static void pyProcedureWrapper(Object... args) throws Exception { PyInterpreterPool pool = null; PythonInterpreter interpreter = null; String setFacFuncName = "setFactory"; String funcName = "execute"; try { int pyScriptIdx; byte[] compiledCode; int nargs = args.length; Integer rsDelim = null; for(int i = 0; i < nargs; ++i){ if(args[i] instanceof ResultSet[]){ rsDelim = i; break; } } // set pyScript if(rsDelim == null){ pyScriptIdx = nargs - 1; } else{ pyScriptIdx = rsDelim - 1; } compiledCode = (byte[]) args[pyScriptIdx]; // set the Object[] to pass into Jython code int j = 0; Object[] pyArgs; if(nargs - 1 ==0){ pyArgs = null; } else{ pyArgs = new Object[nargs - 1]; } for(int i = 0; i < nargs; ++i){ if(i != pyScriptIdx){ pyArgs[j] = args[i]; j++; } } pool = PyInterpreterPool.getInstance(); interpreter = pool.acquire(); PyCodeUtil.exec(compiledCode, interpreter); // add global variable factory, so that the user can use it to construct JDBC ResultSet Object[] factoryArg = {new PyStoredProcedureResultSetFactory()}; PyFunction addFacFunc = interpreter.get(setFacFuncName, PyFunction.class); addFacFunc._jcall(factoryArg); // execute the user defined function, the user needs to fill the ResultSet himself, // just like the original Java Stored Procedure PyFunction userFunc = interpreter.get(funcName, PyFunction.class); if(pyArgs == null){ userFunc.__call__(); }else{ userFunc._jcall(pyArgs); } } catch (Exception e){ throw StandardException.plainWrapException(e); } finally{ if(pool != null && interpreter != null){ pool.release(interpreter); } } }
Example 11
Source File: PyRoutineWrapper.java From spliceengine with GNU Affero General Public License v3.0 | 4 votes |
/** * A wrapper static method for Python user-defined function * * @param args if the registered stored procedure has n parameters, the args[0] to args[n-1] are these parameters. * The args[n] must be a String which contains the Python script. */ public static Object pyFunctionWrapper(Object... args) throws Exception { PyInterpreterPool pool = null; PythonInterpreter interpreter = null; String setFacFuncName = "setFactory"; String funcName = "execute"; PyObject pyResult = null; Object javaResult = null; try { byte[] compiledCode; int nargs = args.length; int pyScriptIdx = args.length - 1; // set pyScript compiledCode = (byte[]) args[pyScriptIdx]; // set the Object[] to pass in Object[] pyArgs; if(nargs - 1 ==0){ pyArgs = null; } else{ pyArgs = new Object[nargs - 1]; System.arraycopy(args, 0, pyArgs, 0, nargs - 1); } pool = PyInterpreterPool.getInstance(); interpreter = pool.acquire(); PyCodeUtil.exec(compiledCode, interpreter); // add global variable factory, so that the user can use it to construct JDBC ResultSet Object[] factoryArg = {new PyStoredProcedureResultSetFactory()}; PyFunction addFacFunc = interpreter.get(setFacFuncName, PyFunction.class); addFacFunc._jcall(factoryArg); // execute the user defined function, the user needs to fill the ResultSet himself, // just like the original Java Stored Procedure PyFunction userFunc = interpreter.get(funcName, PyFunction.class); if(pyArgs == null){ pyResult = userFunc.__call__(); }else{ pyResult = userFunc._jcall(pyArgs); } javaResult = pyResult.__tojava__(Object.class); if(pyResult instanceof PyLong){ // When the result has type PyLong, the result's corresponding // sql type should be BigInt. javaResult = ((BigInteger) javaResult).longValue(); } return javaResult; } catch (Exception e){ throw StandardException.plainWrapException(e); } finally{ if(pool != null && interpreter != null){ pool.release(interpreter); } } }