Class Python


  • public class Python
    extends Object
    Python interpreter.

    If you embed two Python interpreters, many Python libraries do not work correctly. Therefore this class is a singleton class. All the methods are static.

    This class is thread-safe. All the methods are synchronized.

    • Method Detail

      • eval

        public static <T> T eval​(String src)
        Python built-in eval().
        Type Parameters:
        T - The Java class after conversion from Python.
        Parameters:
        src - Python code. This must be a single line code.
        Returns:
        The Java object converted from the Python object.
      • exec

        public static void exec​(String src)
        Python built-in exec().
        Parameters:
        src - Python code. This can be multiple lines code.
      • get

        public static <T> T get​(String name)
        Get the global Python variable and convert it to a Java object.
        Type mappings. Python to Java.
        PythonJava
        Nonenull
        boolboolean
        intlong
        floatdouble
        strString
        scalar np.bool8boolean
        scalar np.int8byte
        scalar np.int16short
        scalar np.uint16char
        scalar np.int32int
        scalar np.int64long
        scalar np.float32float
        scalar np.float64double
        scalar np.datetime64[W, D, h, m, s, ms, us, or ns]Instant
        bytesbyte[]
        bytearraybyte[]
        dictLinkedHashMap
        ndarray np.bool8NpNdarrayBoolean
        ndarray np.int8NpNdarrayByte
        ndarray np.int16NpNdarrayShort
        ndarray np.uint16NpNdarrayChar
        ndarray np.int32NpNdarrayInt
        ndarray np.int64NpNdarrayLong
        ndarray np.float32NpNdarrayFloat
        ndarray np.float64NpNdarrayDouble
        ndarray np.datetime64[W, D, h, m, s, ms, us, or ns]NpNdarrayInstant
        iterableArrayList
        Type Parameters:
        T - The Java class after conversion from Python.
        Parameters:
        name - The variable name
        Returns:
        The Java object converted from the Python object.
        Throws:
        PythonException - If the value cannot convert to a Java object.
        NoSuchElementException - If the variable does not exists.
      • put

        public static void put​(String name,
                               Object value)
        Convert the Java object and set it to the global Python variable.
        Type mappings. Java to Python.
        JavaPython
        nullNone
        booleanbool
        byteint
        shortint
        charint
        intint
        longint
        floatfloat
        doublefloat
        Instantnp.datetime64[ns]
        Stringstr
        byte[]bytes
        boolean[]np.ndarray, dtype=np.bool8
        short[]np.ndarray, dtype=np.int16
        char[]np.ndarray, dtype=np.uint16
        int[]np.ndarray, dtype=np.int32
        long[]np.ndarray, dtype=np.int64
        float[]np.ndarray, dtype=np.float32
        double[]np.ndarray, dtype=np.float64
        Instant[]np.ndarray, dtype=np.datetime64[ns]
        NpNdarrayBooleannp.ndarray, dtype=np.bool8
        NpNdarrayBytenp.ndarray, dtype=np.int8
        NpNdarrayShortnp.ndarray, dtype=np.int16
        NpNdarrayCharnp.ndarray, dtype=np.uint16
        NpNdarrayIntnp.ndarray, dtype=np.int32
        NpNdarrayLongnp.ndarray, dtype=np.int64
        NpNdarrayFloatnp.ndarray, dtype=np.float32
        NpNdarrayDoublenp.ndarray, dtype=np.float64
        NpNdarrayInstantnp.ndarray, dtype=np.datetime64[ns]
        java.util.Mapdict
        scala.collection.Mapdict
        Object[]list
        Iterablelist
        scala.Function0 - Function22built-in global Python function
        Parameters:
        name - The variable name
        value - The value to put.
        Throws:
        PythonException - If the value cannot convert to a Python object.