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

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

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

PythonInterpreter.getLocals介绍

暂无

代码示例

代码示例来源:origin: nodebox/nodebox

public ImmutableMap<String, Function> call() throws Exception {
    // This creates a dependency between function and the client.
    // However, we need to know the load paths before we can do anything, so this is necessary.
    PythonUtils.initializePython();
    Py.getSystemState().path.append(new PyString(file.getParentFile().getCanonicalPath()));
    PythonInterpreter interpreter = new PythonInterpreter();
    try {
      interpreter.execfile(file.getCanonicalPath());
    } catch (IOException e) {
      throw new LoadException(file, e);
    } catch (PyException e) {
      throw new LoadException(file, e);
    }
    PyStringMap map = (PyStringMap) interpreter.getLocals();
    ImmutableMap.Builder<String, Function> builder = ImmutableMap.builder();
    for (Object key : map.keys()) {
      Object o = map.get(Py.java2py(key));
      if (o instanceof PyFunction) {
        String name = (String) key;
        Function f = new PythonFunction(name, (PyFunction) o);
        builder.put(name, f);
      }
    }
    return builder.build();
  }
});

代码示例来源:origin: usc-isi-i2/Web-Karma

private void resetRuntimeLocal() {
  for (String key : localKeys) {
    try {
      this.interpreter.getLocals().__delitem__(key);
    } catch (Exception e) {
      //Key is missing, do nothing
    }
  }
}

代码示例来源:origin: org.python/jython

/**
 * Sets a variable in the local namespace.
 *
 * @param name the name of the variable
 * @param value the Python object to set the variable to
 */
public void set(String name, PyObject value) {
  getLocals().__setitem__(name.intern(), value);
}

代码示例来源:origin: org.python/jython

/**
 * Returns the value of a variable in the local namespace.
 *
 * @param name the name of the variable
 * @return the value of the variable, or null if that name isn't assigned
 */
public PyObject get(String name) {
  return getLocals().__finditem__(name.intern());
}

代码示例来源:origin: org.python/jython

/**
 * Sets a variable in the local namespace.
 *
 * @param name the name of the variable
 * @param value the object to set the variable to (as converted to an appropriate Python object)
 */
public void set(String name, Object value) {
  getLocals().__setitem__(name.intern(), Py.java2py(value));
}

代码示例来源:origin: org.apache.apex/malhar-contrib

@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;
}

代码示例来源:origin: apache/apex-malhar

@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;
}

代码示例来源:origin: CloudSlang/score

private Map<String, Serializable> getPythonLocals() {
  Map<String, Serializable> result = new HashMap<>();
  if(interpreter.getLocals() != null) {
    for (PyObject pyObject : interpreter.getLocals().asIterable()) {
      String key = pyObject.asString();
      PyObject value = interpreter.get(key);
      if (keyIsExcluded(key, value)) {
        continue;
      }
      result.put(key, value);
    }
  }
  return result;
}

代码示例来源:origin: io.cloudslang/runtime-management-impl

private Map<String, Serializable> getPythonLocals() {
  Map<String, Serializable> result = new HashMap<>();
  if(interpreter.getLocals() != null) {
    for (PyObject pyObject : interpreter.getLocals().asIterable()) {
      String key = pyObject.asString();
      PyObject value = interpreter.get(key);
      if (keyIsExcluded(key, value)) {
        continue;
      }
      result.put(key, value);
    }
  }
  return result;
}

代码示例来源:origin: org.python/jython

/**
 * Evaluates a Python code object and returns the result.
 */
public PyObject eval(PyObject code) {
  setSystemState();
  return __builtin__.eval(code, getLocals());
}

代码示例来源:origin: org.python/jython

public <T> T getInterface(Class<T> clazz) {
  return getInterface(new PyModule("__jsr223__", interp.getLocals()), clazz);
}

代码示例来源:origin: usc-isi-i2/Web-Karma

private boolean evaluatePythonExpression(Row r, PyCode code, PythonInterpreter interpreter) {
  evalColumns.clear();
  try {
    ArrayList<Node> nodes = new ArrayList<>(r.getNodes());
    Node node = nodes.get(0);
    interpreter.getLocals().__setitem__("nodeid", new PyString(node.getId()));
    PyObject output = interpreter.eval(code);
    return PythonTransformationHelper.getPyObjectValueAsBoolean(output);
  }catch(Exception e) {
    return onError;
  }
}

代码示例来源:origin: org.python/jython

/**
 * Executes a file of Python source in the local namespace.
 */
public void execfile(String filename) {
  PyObject locals = getLocals();
  setSystemState();
  __builtin__.execfile_flags(filename, locals, locals, cflags);
  Py.flushLine();
}

代码示例来源:origin: org.python/jython

/**
 * Evaluates a string as a Python expression and returns the result.
 */
public PyObject eval(String s) {
  setSystemState();
  return __builtin__.eval(new PyString(s), getLocals());
}

代码示例来源:origin: org.python/jython

/**
 * Executes a Python code object in the local namespace.
 */
public void exec(PyObject code) {
  setSystemState();
  Py.exec(code, getLocals(), null);
  Py.flushLine();
}

代码示例来源:origin: io.cloudslang/runtime-management-impl

private PythonExecutionResult exec(String script) {
  interpreter.exec(script);
  Iterator<PyObject> localsIterator = interpreter.getLocals().asIterable().iterator();
  Map<String, Serializable> returnValue = new HashMap<>();
  while (localsIterator.hasNext()) {
    String key = localsIterator.next().asString();
    PyObject value = interpreter.get(key);
    if (keyIsExcluded(key, value)) {
      continue;
    }
    Serializable javaValue = resolveJythonObjectToJavaExec(value, key);
    returnValue.put(key, javaValue);
  }
  return new PythonExecutionResult(returnValue);
}

代码示例来源:origin: CloudSlang/score

private PythonExecutionResult exec(String script) {
  interpreter.exec(script);
  Iterator<PyObject> localsIterator = interpreter.getLocals().asIterable().iterator();
  Map<String, Serializable> returnValue = new HashMap<>();
  while (localsIterator.hasNext()) {
    String key = localsIterator.next().asString();
    PyObject value = interpreter.get(key);
    if (keyIsExcluded(key, value)) {
      continue;
    }
    Serializable javaValue = resolveJythonObjectToJavaExec(value, key);
    returnValue.put(key, javaValue);
  }
  return new PythonExecutionResult(returnValue);
}

代码示例来源:origin: co.cask.hydrator/core-plugins

@Override
public void transform(StructuredRecord input, final Emitter<StructuredRecord> emitter) {
 try {
  Emitter<Map> pythonEmitter = new PythonEmitter(emitter, schema == null ? input.getSchema() : schema);
  interpreter.set(INPUT_STRUCTURED_RECORD_VARIABLE_NAME, encode(input, input.getSchema()));
  interpreter.set(EMITTER_VARIABLE_NAME, pythonEmitter);
  Py.runCode(compiledScript, interpreter.getLocals(), interpreter.getLocals());
 } catch (PyException e) {
  // We put the stack trace as the exception message, because otherwise the information from PyException is lost.
  // PyException only exposes the actual cause (Python stack trace) if printStackTrace() is called on it.
  throw new IllegalArgumentException("Could not transform input.\n" + getStackTrace(e));
 }
}

代码示例来源:origin: org.python/jython

/**
 * Executes a string of Python source in the local namespace.
 */
public void exec(String s) {
  setSystemState();
  Py.exec(Py.compile_flags(s, "<string>", CompileMode.exec, cflags), getLocals(), null);
  Py.flushLine();
}

代码示例来源:origin: org.python/jython

public void execfile(java.io.InputStream s, String name) {
  setSystemState();
  Py.runCode(Py.compile_flags(s, name, CompileMode.exec, cflags), null, getLocals());
  Py.flushLine();
}

相关文章