Java Code Examples for org.python.core.PyStringMap

The following examples show how to use org.python.core.PyStringMap. These examples are extracted from open source projects. 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 Project: spork   Source File: JythonScriptEngine.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Object> getParamsFromVariables() throws IOException {
    PyFrame frame = Py.getFrame();
    @SuppressWarnings("unchecked")
    List<PyTuple> locals = ((PyStringMap) frame.getLocals()).items();
    Map<String, Object> vars = new HashMap<String, Object>();
    for (PyTuple item : locals) {
        String key = (String) item.get(0);
        Object obj = item.get(1);
        if (obj != null) {
            String value = item.get(1).toString();
            vars.put(key, value);
        }
    }
    return vars;
}
 
Example 2
Source Project: MeteoInfo   Source File: FrmMain.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Delete variables
 */
public void delVariables() {
    PythonInteractiveInterpreter interp = this.consoleDock.getInterpreter();
    PyStringMap locals = (PyStringMap) interp.getLocals();
    PyList items = locals.items();
    String name;
    for (Object a : items) {
        PyTuple at = (PyTuple) a;
        name = at.__getitem__(0).toString();
        if (!this.loadObjects.contains(name)) {
            locals.__delitem__(name);
        }
    }
}
 
Example 3
Source Project: attic-apex-malhar   Source File: PythonOperator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> getBindings()
{
  Map<String, Object> bindings = new HashMap<String, Object>();
  PyStringMap keyValueMap = (PyStringMap)interp.getLocals();
  PyIterator keyValueSet = (PyIterator)keyValueMap.iteritems();
  for (Object temp : keyValueSet) {
    PyTuple tempEntry = (PyTuple)temp;
    Iterator<PyObject> iter = tempEntry.iterator();
    bindings.put((String)iter.next().__tojava__(String.class), iter.next());
  }
  return bindings;
}
 
Example 4
Source Project: score   Source File: PythonExecutor.java    License: Apache License 2.0 4 votes vote down vote up
private void initInterpreter() {
    interpreter.setLocals(new PyStringMap());
}
 
Example 5
Source Project: spork   Source File: JythonScriptEngine.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * get the state of modules currently loaded
 * @return a map of module name to module file (absolute path)
 */
private static Map<String, String> getModuleState() {
    // determine the current module state
    Map<String, String> files = new HashMap<String, String>();
    PyStringMap modules = (PyStringMap) Py.getSystemState().modules;
    for (PyObject kvp : modules.iteritems().asIterable()) {
        PyTuple tuple = (PyTuple) kvp;
        String name = tuple.get(0).toString();
        Object value = tuple.get(1);
        // inspect the module to determine file location and status
        try {
            Object fileEntry = null;
            Object loader = null;
            if (value instanceof PyJavaPackage ) {
                fileEntry = ((PyJavaPackage) value).__file__;
            } else if (value instanceof PyObject) {
                // resolved through the filesystem (or built-in)
                PyObject dict = ((PyObject) value).getDict();
                if (dict != null) {
                    fileEntry = dict.__finditem__("__file__");
                    loader = dict.__finditem__("__loader__");
                } // else built-in
            }   // else some system module?

            if (fileEntry != null) {
                File file = resolvePyModulePath(fileEntry.toString(), loader);
                if (file.exists()) {
                    String apath = file.getAbsolutePath();
                    if (apath.endsWith(".jar") || apath.endsWith(".zip")) {
                        // jar files are simple added to the pigContext
                        files.put(apath, apath);
                    } else {
                        // determine the relative path that the file should have in the jar
                        int pos = apath.lastIndexOf(File.separatorChar + name.replace('.', File.separatorChar));
                        if (pos > 0) {
                            files.put(apath.substring(pos + 1), apath);
                        } else {
                            files.put(apath, apath);
                        }
                    }
                } else {
                    LOG.warn("module file does not exist: " + name + ", " + file);
                }
            } // else built-in
        } catch (Exception e) {
            LOG.warn("exception while retrieving module state: " + value, e);
        }
    }
    return files;
}