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
    • getKeysRaw

      Stream<PyObj> getKeysRaw()
      Retrieves all keys from the give scope.

      No conversion is done.

      Technically, the keys can be tuples...

      Returns:
      the keys
    • getEntriesRaw

      Stream<Map.Entry<PyObj,​PyObj>> getEntriesRaw()
      Retrieves all keys and values from the given scope.

      No conversion is done.

      Returns:
      the keys and values
    • 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. Keys 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)
      Equivalent to getValueRaw(String).map(convertValue(PyObj)(PyObj))
      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
    • getKeys

      default Stream<String> getKeys()
      Equivalent to getKeysRaw().map(convertStringKey(PyObj)(PyObj))
      Returns:
      the string keys
    • getEntries

      default Stream<Map.Entry<String,​Object>> getEntries()
      Equivalent to getEntriesRaw(), where the keys have been converted via convertStringKey(PyObj)(PyObj) and the values via convertValue(PyObj)(PyObj)
      Returns:
      the string keys and converted values
    • getKeysCollection

      default Collection<String> getKeysCollection()
      Equivalent to getKeys().collect(someCollector)
      Returns:
      the string keys, as a collection
    • getEntriesMap

      default Map<String,​Object> getEntriesMap()
      Equivalent to getEntries().collect(someMapCollector)
      Returns:
      the string keys and converted values, as a map
    • globals

      org.jpy.PyDictWrapper globals()