Class PyUtilities

java.lang.Object
com.inductiveautomation.ignition.common.PyUtilities

public final class PyUtilities extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    isIterable(@NonNull org.python.core.PyObject pyObject)
    Predicate that returns true if the provided object is iterable - which is a superset of sequences, as an iterable doesn't have to be sized.
    static boolean
    isSequence(@NonNull org.python.core.PyObject pyObject)
    Predicate that returns true if the provided object is a sequence, per Java convention - meaning, strings are not considered sequences.
    static boolean
    isString(@NonNull org.python.core.PyObject pyObject)
    Simple predicate that returns true if the provided object is a Python string (ie, inherits from PyBaseString.
    static org.python.core.PyException
    javaError(@NonNull org.python.core.PyObject type, @NonNull Throwable error)
    Translates a Java exception into a Python exception, retaining stacktrace information.
    static Stream<org.python.core.PyObject>
    stream(@NonNull org.python.core.PyObject pyObject)
    Stream items from pyObject.
    static Stream<org.apache.commons.lang3.tuple.Pair<org.python.core.PyObject,org.python.core.PyObject>>
    streamEntries(@NonNull org.python.core.PyObject pyObject)
    Stream entries from pyObject, in a key: value Pair.
    static Collector<org.apache.commons.lang3.tuple.Pair<org.python.core.PyObject,org.python.core.PyObject>,Object,org.python.core.PyDictionary>
    Collect a stream of Pairs of PyObjects into a new PyDictionary.
    static <T> Collector<T,Object,org.python.core.PyDictionary>
    toPyDictionary(@NonNull Function<T,?> keyExtractor, @NonNull Function<T,?> valueExtractor)
    Collect a stream of T into a new PyDictionary.
    static Collector<org.python.core.PyObject,Object,org.python.core.PyList>
    Collect a stream into a new PyList.
    static org.python.core.PyTuple
    tupleOf(Object @NonNull ... items)
    Constructs a new PyTuple instance from the provided items.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isString

      public static boolean isString(@NonNull org.python.core.PyObject pyObject)
      Simple predicate that returns true if the provided object is a Python string (ie, inherits from PyBaseString.
    • isSequence

      public static boolean isSequence(@NonNull org.python.core.PyObject pyObject)
      Predicate that returns true if the provided object is a sequence, per Java convention - meaning, strings are not considered sequences.
    • isIterable

      public static boolean isIterable(@NonNull org.python.core.PyObject pyObject)
      Predicate that returns true if the provided object is iterable - which is a superset of sequences, as an iterable doesn't have to be sized. Returns true for strings as well.
    • stream

      public static Stream<org.python.core.PyObject> stream(@NonNull org.python.core.PyObject pyObject)
      Stream items from pyObject. Per Python behavior, will stream characters one-by-one in a string.
      Throws:
      org.python.core.PyException - TypeError if provided PyObject is not iterable.
    • streamEntries

      public static Stream<org.apache.commons.lang3.tuple.Pair<org.python.core.PyObject,org.python.core.PyObject>> streamEntries(@NonNull org.python.core.PyObject pyObject)
      Stream entries from pyObject, in a key: value Pair.
      Throws:
      org.python.core.PyException - TypeError if provided PyObject is not iterable.
      IllegalArgumentException - if provided PyObject is not a mapping type.
    • toPyList

      public static Collector<org.python.core.PyObject,Object,org.python.core.PyList> toPyList()
      Collect a stream into a new PyList.
    • toPyDictionary

      public static <T> Collector<T,Object,org.python.core.PyDictionary> toPyDictionary(@NonNull Function<T,?> keyExtractor, @NonNull Function<T,?> valueExtractor)
      Collect a stream of T into a new PyDictionary.
      Type Parameters:
      T - The incoming type, eg Map.Entry or Pair.
      Parameters:
      keyExtractor - The key extractor, eg Map.Entry.getKey(). Should not return a PyObject directly.
      valueExtractor - The value extractor, eg Map.Entry.getValue(). Should not return a PyObject directly.
      Returns:
      a new PyDictionary.
    • toPyDictionary

      public static Collector<org.apache.commons.lang3.tuple.Pair<org.python.core.PyObject,org.python.core.PyObject>,Object,org.python.core.PyDictionary> toPyDictionary()
      Collect a stream of Pairs of PyObjects into a new PyDictionary.
      Returns:
      a new PyDictionary.
    • tupleOf

      public static org.python.core.PyTuple tupleOf(Object @NonNull ... items)
      Constructs a new PyTuple instance from the provided items. Each item in the input array is converted to a PyObject using Py.java2py.
      Parameters:
      items - the objects to be included in the tuple; must not be null.
    • javaError

      public static org.python.core.PyException javaError(@NonNull org.python.core.PyObject type, @NonNull Throwable error)
      Translates a Java exception into a Python exception, retaining stacktrace information. Unlike Py.JavaError(Throwable), Jython callers can catch exceptions thrown as the provided exception type.
      Parameters:
      type - The Python exception type, eg Py.ValueError.
      error - The Java exception.
      Returns:
      a new PyException to throw.
      See Also:
      • Py.JavaError(Throwable)