Interface PythonScope<PyObj>

Type Parameters:
PyObj - the implementation's raw Python object type
All Known Implementing Classes:
PythonScopeJpyImpl

public interface PythonScope<PyObj>
A collection of methods around retrieving objects from the given Python scope.

The scope is likely coming from some sort of Python dictionary. The scope might be local, global, or other.

  • Method Details

    • getValueRaw

      Optional<PyObj> getValueRaw(String name)
      Retrieves a value from the given scope.

      No conversion is done.

      Parameters:
      name - the name of the python variable
      Returns:
      the value, or empty
    • convertStringKey

      String convertStringKey(PyObj key)
      The helper method to turn a raw key into a string key.

      Note: this assumes that all the keys are strings, which is not always true. Indices can also be tuples. TODO: revise interface as appropriate if this becomes an issue.

      Parameters:
      key - the raw key
      Returns:
      the string key
      Throws:
      IllegalArgumentException - if the key is not a string
    • convertValue

      Object convertValue(PyObj value)
      The helper method to turn a raw value into an implementation specific object.

      This method should NOT convert PyObj of None type to null - we need to preserve the None object so it works with other Optional return values.

      Parameters:
      value - the raw value
      Returns:
      the converted object value
    • containsKey

      default boolean containsKey(String name)
      Finds out if a variable is in scope
      Parameters:
      name - the name of the python variable
      Returns:
      true iff the scope contains the variable
    • getValue

      default Optional<Object> getValue(String name)
      Parameters:
      name - the name of the python variable
      Returns:
      the converted object value, or empty
    • getValue

      default <T> Optional<T> getValue(String name, Class<T> clazz)
      Equivalent to getValue(String).map(clazz.Class.cast(Object))
      Type Parameters:
      T - the return type
      Parameters:
      name - the name of the python variable
      clazz - the class to cast to
      Returns:
      the converted casted value, or empty
    • getValueUnchecked

      default <T> Optional<T> getValueUnchecked(String name)
      Equivalent to getValue(String).map(x -> (T)x);
      Type Parameters:
      T - the return type
      Parameters:
      name - the name of the python variable
      Returns:
      the converted casted value, or empty
    • mainGlobals

      org.jpy.PyDictWrapper mainGlobals()
      Returns:
      the Python's __main__ module namespace
    • currentScope

      org.jpy.PyDictWrapper currentScope()
      Returns:
      the current scope or the main globals if no scope is set
    • pushScope

      void pushScope(org.jpy.PyObject pydict)
      Push the provided Python scope into the thread scope stack for subsequent operations on Tables
      Parameters:
      pydict - a Python dictionary representing the current scope under which the Python code in running, it is the combination of module globals and function locals
    • popScope

      void popScope()
      Pop the last Python scope pushed by pushScope(PyObject pydict) from the thread scope stack