Search in sources :

Example 26 with PyObject

use of org.python.core.PyObject in project score by CloudSlang.

the class EmbeddedPythonExecutorWrapper method doEval.

private Serializable doEval(String prepareEnvironmentScript, String script) {
    // Set boolean values
    pythonInterpreter.set("true", Boolean.TRUE);
    pythonInterpreter.set("false", Boolean.FALSE);
    // Prepare environment if required
    if (isNotEmpty(prepareEnvironmentScript)) {
        pythonInterpreter.exec(prepareEnvironmentScript);
    }
    PyObject evalResultPyObject = pythonInterpreter.eval(script);
    return resolveJythonObjectToJavaForEval(evalResultPyObject, script);
}
Also used : PyObject(org.python.core.PyObject)

Example 27 with PyObject

use of org.python.core.PyObject in project score by CloudSlang.

the class EmbeddedPythonExecutorWrapper method processExecResults.

private PythonExecutionResult processExecResults() {
    Iterator<PyObject> localsIterator = pythonInterpreter.getLocals().asIterable().iterator();
    Map<String, Serializable> returnValue = new HashMap<>();
    while (localsIterator.hasNext()) {
        PyObject next = localsIterator.next();
        String key = next.asString();
        PyObject value = pythonInterpreter.get(key);
        if (!isLocalEntryExcluded(key, value)) {
            returnValue.put(key, resolveJythonObjectToJavaForExec(value, key));
        }
    }
    return new PythonExecutionResult(returnValue);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) PyString(org.python.core.PyString) PyObject(org.python.core.PyObject) PythonExecutionResult(io.cloudslang.runtime.api.python.PythonExecutionResult)

Example 28 with PyObject

use of org.python.core.PyObject in project MeteoInfo by meteoinfo.

the class FrmMain method loadApplication.

/**
 * 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());
}
Also used : PythonInterpreter(org.python.util.PythonInterpreter) PyObject(org.python.core.PyObject) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) IPlugin(org.meteoinfo.ui.plugin.IPlugin)

Example 29 with PyObject

use of org.python.core.PyObject in project MeteoInfo by meteoinfo.

the class FrmAppsManager method readPyApp.

// GEN-LAST:event_formWindowClosed
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;
}
Also used : PythonInterpreter(org.python.util.PythonInterpreter) Application(org.meteoinfo.lab.application.Application) PyObject(org.python.core.PyObject) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IPlugin(org.meteoinfo.ui.plugin.IPlugin)

Example 30 with PyObject

use of org.python.core.PyObject in project scisoft-core by DawnScience.

the class PythonUtilsTest method testCompile.

private void testCompile(boolean isSingle) throws Exception {
    // NB "eval" is not available in Jython nor documented in CPython
    CompileMode mode = isSingle ? CompileMode.single : CompileMode.exec;
    String filename = "<input>";
    CompilerFlags flags = new CompilerFlags();
    PyObject ret = Py.compile_command_flags("", filename, mode, flags, false);
    System.out.println(ret);
    ret = Py.compile_command_flags("1", filename, mode, flags, false);
    System.out.println(ret);
    ret = Py.compile_command_flags("print 2", filename, mode, flags, false);
    System.out.println(ret);
    ret = Py.compile_command_flags("if True: print 42", filename, mode, flags, false);
    if (isSingle) {
        assertEquals(Py.None, ret);
    }
    System.out.println(ret);
    try {
        ret = Py.compile_command_flags("if True:", filename, mode, flags, false);
        if (isSingle) {
            assertEquals(Py.None, ret);
        } else {
            fail();
        }
        System.out.println(ret);
    } catch (Exception e) {
    }
}
Also used : CompileMode(org.python.core.CompileMode) CompilerFlags(org.python.core.CompilerFlags) PyObject(org.python.core.PyObject) DatasetException(org.eclipse.january.DatasetException)

Aggregations

PyObject (org.python.core.PyObject)75 PyString (org.python.core.PyString)24 PyList (org.python.core.PyList)15 ArrayList (java.util.ArrayList)14 PyException (org.python.core.PyException)12 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 PyInteger (org.python.core.PyInteger)8 PythonInterpreter (org.python.util.PythonInterpreter)7 Serializable (java.io.Serializable)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 PyFloat (org.python.core.PyFloat)6 PyStringMap (org.python.core.PyStringMap)6 List (java.util.List)5 Map (java.util.Map)5 Test (org.junit.Test)5 PyDictionary (org.python.core.PyDictionary)5 PyLong (org.python.core.PyLong)5 PyNone (org.python.core.PyNone)5 PyTuple (org.python.core.PyTuple)5