org.jpy

## Class PyObject

- java.lang.Object
- - org.jpy.PyObject

- All Implemented Interfaces:
    AutoCloseable
  Direct Known Subclasses:
    PyModule

      public class PyObject
      extends Object
      implements AutoCloseable

  Represents a Python object (of Python/C API type `PyObject*`) in the Python interpreter.
  Since:
    0.7

- - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `PyDictWrapper` | `asDict()`
    `List<PyObject>` | `asList()`
    `<T> T` | `call(Class<T> returnType, String name, Class<?>[] paramTypes, Object[] args)`
    `<T,A0> T` | `call(Class<T> returnType, String name, Class<A0> clazz0, A0 arg0)`
    `<T,A0,A1> T` | `call(Class<T> returnType, String name, Class<A0> clazz0, A0 arg0, Class<A1> clazz1, A1 arg1)`
    `<T,A0,A1,A2> T` | `call(Class<T> returnType, String name, Class<A0> clazz0, A0 arg0, Class<A1> clazz1, A1 arg1, Class<A2> clazz2, A2 arg2)`
    `PyObject` | `call(String name, Object... args)` Call the callable Python object with the given name and arguments.
    `PyObject` | `callMethod(String name, Object... args)` Call the callable Python method with the given name and arguments.
    `static int` | `cleanup()`
    `void` | `close()` Decreases the reference count of `this` on first invocation.
    `<T> T` | `createProxy(Class<T> type)` Create a Java proxy instance of this Python object which contains compatible methods to the ones provided in the interface given by the `type` parameter.
    `Object` | `createProxy(PyLib.CallableKind callableKind, Class<?>... types)` Create a Java proxy instance of this Python object (or module) which contains compatible methods (or functions) to the ones provided in the interfaces given by all the `type` parameters.
    `void` | `delAttribute(String name)` Deletes the value of a Python attribute.
    `boolean` | `eq(Object other)`
    `boolean` | `equals(Object o)` Indicates whether some other object is "equal to" this one.
    `static PyObject` | `executeCode(String code, PyInputMode mode)` Executes Python source code.
    `static PyObject` | `executeCode(String code, PyInputMode mode, Object globals, Object locals)` Executes Python source code in the context specified by the `globals` and `locals` maps.
    `static PyObject` | `executeScript(String script, PyInputMode mode)` Executes Python source script.
    `static PyObject` | `executeScript(String script, PyInputMode mode, Object globals, Object locals)` Executes Python source script in the context specified by the `globals` and `locals` maps.
    `PyObject` | `getAttribute(String name)` Gets the Python value of a Python attribute.
    `<T> T` | `getAttribute(String name, Class<? extends T> valueType)` Gets the value of a Python attribute as Java object of a given type.
    `boolean` | `getBooleanValue()`
    `double` | `getDoubleValue()`
    `int` | `getIntValue()`
    `long` | `getLongValue()`
    `<T> T[]` | `getObjectArrayValue(Class<? extends T> itemType)` Gets this Python object as Java `Object[]` value of the given item type.
    `Object` | `getObjectValue()` Gets this Python object as Java `Object` value.
    `long` | `getPointer()`
    `String` | `getStringValue()`
    `PyObject` | `getType()` Gets the Python type object for this wrapped object.
    `boolean` | `hasAttribute(String name)` Checks for the existence of a Python attribute..
    `long` | `hash()` Runs the python hash function on this object.
    `int` | `hashCode()` Computes a hash code from this object's pointer value.
    `boolean` | `isBoolean()`
    `boolean` | `isCallable()`
    `boolean` | `isConvertible()`
    `boolean` | `isDict()`
    `boolean` | `isFloat()`
    `boolean` | `isFunction()`
    `boolean` | `isInt()`
    `boolean` | `isList()`
    `boolean` | `isLong()`
    `boolean` | `isModule()`
    `boolean` | `isNone()`
    `boolean` | `isString()`
    `boolean` | `isTuple()`
    `String` | `repr()` Gets a the python repr of this object
    `<T> void` | `setAttribute(String name, T value)` Sets the value of a Python attribute from the given Java object.
    `<T> void` | `setAttribute(String name, T value, Class<? extends T> valueType)` Sets the value of a Python attribute from the given Java object and Java type (if given).
    `String` | `str()` Runs the python str function on this object.
    `String` | `toString()` Gets the python string representation of this object.
    `static PyObject` | `unwrapProxy(Object object)` Unwraps the original Python object used to create `object`.

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Method Detail

    - #### cleanup

          public static int cleanup()

    - #### close

          public final void close()

      Decreases the reference count of `this` on first invocation.
      Specified by:
        `close` in interface `AutoCloseable`

    - #### executeCode

          public static PyObject executeCode(String code,
                                             PyInputMode mode)

      Executes Python source code.
      Parameters:
        `code` - The Python source code.
        `mode` - The execution mode.
      Returns:
        The result of executing the code as a Python object.

    - #### executeScript

          public static PyObject executeScript(String script,
                                               PyInputMode mode)
                                        throws FileNotFoundException

      Executes Python source script.
      Parameters:
        `script` - The Python source script.
        `mode` - The execution mode.
      Returns:
        The result of executing the script as a Python object.
      Throws:
        `FileNotFoundException`

    - #### executeCode

          public static PyObject executeCode(String code,
                                             PyInputMode mode,
                                             Object globals,
                                             Object locals)

      Executes Python source code in the context specified by the `globals` and `locals` maps.

      If a Java value in the `globals` and `locals` maps cannot be directly converted into a Python object, a Java wrapper will be created instead. If a Java value is a wrapped Python object of type `PyObject`, it will be unwrapped.

      Parameters:
        `code` - The Python source code.
        `mode` - The execution mode.
        `globals` - The global variables to be set, or `null`.
        `locals` - The locals variables to be set, or `null`.
      Returns:
        The result of executing the code as a Python object.

    - #### executeScript

          public static PyObject executeScript(String script,
                                               PyInputMode mode,
                                               Object globals,
                                               Object locals)
                                        throws FileNotFoundException

      Executes Python source script in the context specified by the `globals` and `locals` maps.

      If a Java value in the `globals` and `locals` maps cannot be directly converted into a Python object, a Java wrapper will be created instead. If a Java value is a wrapped Python object of type `PyObject`, it will be unwrapped.

      Parameters:
        `script` - The Python source script.
        `mode` - The execution mode.
        `globals` - The global variables to be set, or `null`.
        `locals` - The locals variables to be set, or `null`.
      Returns:
        The result of executing the script as a Python object.
      Throws:
        `FileNotFoundException` - if the script file is not found

    - #### getPointer

          public final long getPointer()

      Returns:
        A unique pointer to the wrapped Python object.

    - #### getIntValue

          public int getIntValue()

      Returns:
        This Python object as a Java `int` value.

    - #### getLongValue

          public long getLongValue()

      Returns:
        This Python object as a Java `long` value.

    - #### getBooleanValue

          public boolean getBooleanValue()

      Returns:
        This Python object as a Java `boolean` value.

    - #### getDoubleValue

          public double getDoubleValue()

      Returns:
        This Python object as a Java `double` value.

    - #### getStringValue

          public String getStringValue()

      Returns:
        This Python object as a Java `String` value.

    - #### getObjectValue

          public Object getObjectValue()

      Gets this Python object as Java `Object` value.

      If this Python object cannot be converted into a Java object, a Java wrapper of type `PyObject` will be returned. If this Python object is a wrapped Java object, it will be unwrapped.

      Returns:
        This Python object as a Java `Object` value.

    - #### getType

          public PyObject getType()

      Gets the Python type object for this wrapped object.
      Returns:
        This Python object's type as a `PyObject` wrapped value.

    - #### isDict

          public boolean isDict()

    - #### isList

          public boolean isList()

    - #### isBoolean

          public boolean isBoolean()

    - #### isLong

          public boolean isLong()

    - #### isInt

          public boolean isInt()

    - #### isNone

          public boolean isNone()

    - #### isFloat

          public boolean isFloat()

    - #### isCallable

          public boolean isCallable()

    - #### isFunction

          public boolean isFunction()

    - #### isModule

          public boolean isModule()

    - #### isTuple

          public boolean isTuple()

    - #### isString

          public boolean isString()

    - #### isConvertible

          public boolean isConvertible()

    - #### asList

          public List<PyObject> asList()

    - #### asDict

          public PyDictWrapper asDict()

    - #### getObjectArrayValue

          public <T> T[] getObjectArrayValue(Class<? extends T> itemType)

      Gets this Python object as Java `Object[]` value of the given item type. Appropriate type conversions from Python to Java will be performed as needed.

      If a Python item value cannot be converted into a Java item object, a Java wrapper of type `PyObject` will be returned. If a Python item value is a wrapped Java object, it will be unwrapped. If this Python object value is a wrapped Java array object of given type, it will be unwrapped.

      Type Parameters:
        `T` - The expected item type name.
      Parameters:
        `itemType` - The expected item type class.
      Returns:
        This Python object as a Java `Object[]` value.

    - #### getAttribute

          public PyObject getAttribute(String name)

      Gets the Python value of a Python attribute.

      If the Python value cannot be converted into a Java object, a Java wrapper of type `PyObject` will be returned. If the Python value is a wrapped Java object, it will be unwrapped.

      Parameters:
        `name` - A name of the Python attribute.
      Returns:
        A wrapper for the returned Python attribute value.

    - #### getAttribute

          public <T> T getAttribute(String name,
                                    Class<? extends T> valueType)

      Gets the value of a Python attribute as Java object of a given type. Appropriate type conversions from Python to Java will be performed using the given value type.

      If the Python value cannot be converted into a Java object, a Java wrapper of type `PyObject` will be returned. If the Python value is a wrapped Java object, it will be unwrapped.

      Type Parameters:
        `T` - The expected value type name.
      Parameters:
        `name` - A name of the Python attribute.
        `valueType` - The type of the value or `null`, if unknown.
      Returns:
        The Python attribute value as Java object.

    - #### setAttribute

          public <T> void setAttribute(String name,
                                       T value)

      Sets the value of a Python attribute from the given Java object.

      If the Java `value` cannot be directly converted into a Python object, a Java wrapper will be created instead. If the Java `value` is a wrapped Python object of type `PyObject`, it will be unwrapped.

      Type Parameters:
        `T` - The value type name.
      Parameters:
        `name` - A name of the Python attribute.
        `value` - The new attribute value as Java object. May be `null`.

    - #### delAttribute

          public void delAttribute(String name)

      Deletes the value of a Python attribute.
      Parameters:
        `name` - the name of the Python attribute.

    - #### hasAttribute

          public boolean hasAttribute(String name)

      Checks for the existence of a Python attribute..
      Parameters:
        `name` - the name of the Python attribute.
      Returns:
        whether this attribute exists for this object

    - #### setAttribute

          public <T> void setAttribute(String name,
                                       T value,
                                       Class<? extends T> valueType)

      Sets the value of a Python attribute from the given Java object and Java type (if given). Appropriate type conversions from Java to Python will be performed using the given value type.

      If the Java `value` cannot be directly converted into a Python object, a Java wrapper will be created instead. If the Java `value` is a wrapped Python object of type `PyObject`, it will be unwrapped.

      Type Parameters:
        `T` - The value type name.
      Parameters:
        `name` - A name of the Python attribute.
        `value` - The new attribute value as Java object.
        `valueType` - The value type used for the conversion. May be `null`.

    - #### callMethod

          public PyObject callMethod(String name,
                                     Object... args)

      Call the callable Python method with the given name and arguments.

      If a Java value in `args` cannot be directly converted into a Python object, a Java wrapper will be created instead. If the Java value in `args` is a wrapped Python object of type `PyObject`, it will be unwrapped.

      Parameters:
        `name` - A name of a Python attribute that evaluates to a callable object.
        `args` - The arguments for the method call.
      Returns:
        A wrapper for the returned Python object.

    - #### call

          public PyObject call(String name,
                               Object... args)

      Call the callable Python object with the given name and arguments.

      If a Java value in `args` cannot be directly converted into a Python object, a Java wrapper will be created instead. If the Java value in `args` is a wrapped Python object of type `PyObject`, it will be unwrapped.

      Parameters:
        `name` - A name of a Python attribute that evaluates to a callable object,
        `args` - The arguments for the call.
      Returns:
        A wrapper for the returned Python object.

    - #### call

          public <T> T call(Class<T> returnType,
                            String name,
                            Class<?>[] paramTypes,
                            Object[] args)

    - #### call

          public <T,A0> T call(Class<T> returnType,
                               String name,
                               Class<A0> clazz0,
                               A0 arg0)

    - #### call

          public <T,A0,A1> T call(Class<T> returnType,
                                  String name,
                                  Class<A0> clazz0,
                                  A0 arg0,
                                  Class<A1> clazz1,
                                  A1 arg1)

    - #### call

          public <T,A0,A1,A2> T call(Class<T> returnType,
                                     String name,
                                     Class<A0> clazz0,
                                     A0 arg0,
                                     Class<A1> clazz1,
                                     A1 arg1,
                                     Class<A2> clazz2,
                                     A2 arg2)

    - #### createProxy

          public <T> T createProxy(Class<T> type)

      Create a Java proxy instance of this Python object which contains compatible methods to the ones provided in the interface given by the `type` parameter.
      Type Parameters:
        `T` - The interface name.
      Parameters:
        `type` - The interface class.
      Returns:
        A (proxy) instance implementing the given interface.

    - #### createProxy

          public Object createProxy(PyLib.CallableKind callableKind,
                                    Class<?>... types)

      Create a Java proxy instance of this Python object (or module) which contains compatible methods (or functions) to the ones provided in the interfaces given by all the `type` parameters.
      Parameters:
        `callableKind` - The kind of calls to be made.
        `types` - The interface types.
      Returns:
        A instance implementing the all the given interfaces which serves as a proxy for the given Python object (or module).

    - #### unwrapProxy

          public static PyObject unwrapProxy(Object object)

      Unwraps the original Python object used to create `object`. The inverse of `createProxy(Class)`.
      Parameters:
        `object` - The object that may be a proxy.
      Returns:
        The Python object, or null if the object is not a proxy.

    - #### toString

          public final String toString()

      Gets the python string representation of this object.
      Overrides:
        `toString` in class `Object`
      Returns:
        A string representation of the object.
      See Also:
        `getPointer()`

    - #### repr

          public final String repr()

      Gets a the python repr of this object
      Returns:
        A string representation of the object.
      See Also:
        `getPointer()`

    - #### str

          public final String str()

      Runs the python str function on this object.
      Returns:
        The String representation of this object.

    - #### hash

          public final long hash()

      Runs the python hash function on this object.
      Returns:
        The hash.

    - #### eq

          public final boolean eq(Object other)

    - #### equals

          public final boolean equals(Object o)

      Indicates whether some other object is "equal to" this one.
      Overrides:
        `equals` in class `Object`
      Parameters:
        `o` - The other object.
      Returns:
        `true` if the other object is an instance of `PyObject` and if their pointers are equal, `false` otherwise.
      See Also:
        `getPointer()`

    - #### hashCode

          public final int hashCode()

      Computes a hash code from this object's pointer value.
      Overrides:
        `hashCode` in class `Object`
      Returns:
        A hash code value for this object.
      See Also:
        `getPointer()`

Copyright © 2014–2022 Brockmann Consult GmbH. All rights reserved.