Package jep.python

Class PyCallable

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class PyCallable
    extends PyObject
    A Java object that wraps a pointer to a Python callable. These objects can be instance methods, functions, lambdas, or any Python object implementing the __call__ method.

    Instance Method Example:

     
         jep.exec("class Example(object):\n" +
                  "    def __init__(self):\n" +
                  "        pass\n" +
                  "    def helloWorld(self):\n" +
                  "        return 'Hello World'\n");
         jep.exec("instance = Example()");
         PyObject pyobj = jep.getValue("instance", PyObject.class);
         PyCallable pyHelloWorld = PyObject.getAttr("helloWorld", PyCallable.class);
         String result = (String) pyHelloWorld.call();
     
     

    Function Example:

     
         jep.exec("def hello(arg):\n" +
                  "    return 'Hello ' +  str(arg)");
         PyCallable pyHello = jep.getValue("hello", PyCallable.class);
         String result = (String) pyHello.call("World");
     
     
    Since:
    3.8
    See Also:
    Python 2 Call Expression, Python 3 Call Expression
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object call​(java.lang.Object... args)
      Invokes this callable with the args in order.
      java.lang.Object call​(java.lang.Object[] args, java.util.Map<java.lang.String,​java.lang.Object> kwargs)
      Invokes this callable with positional args and keyword args.
      java.lang.Object call​(java.util.Map<java.lang.String,​java.lang.Object> kwargs)
      Invokes this callable with keyword args.
      <T> T callAs​(java.lang.Class<T> expectedType, java.lang.Object... args)
      Invokes this callable with the args in order, converting the return value to the given class.
      <T> T callAs​(java.lang.Class<T> expectedType, java.lang.Object[] args, java.util.Map<java.lang.String,​java.lang.Object> kwargs)
      Invokes this callable with positional args and keyword args, converting the return value to the given class.
      <T> T callAs​(java.lang.Class<T> expectedType, java.util.Map<java.lang.String,​java.lang.Object> kwargs)
      Invokes this callable with keyword args, converting the return value to the given class.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • call

        public java.lang.Object call​(java.lang.Object... args)
                              throws JepException
        Invokes this callable with the args in order.
        Parameters:
        args - args to pass to the function in order
        Returns:
        an Object value
        Throws:
        JepException - if an error occurs
      • callAs

        public <T> T callAs​(java.lang.Class<T> expectedType,
                            java.lang.Object... args)
                     throws JepException
        Invokes this callable with the args in order, converting the return value to the given class.
        Type Parameters:
        T - the generic type of the return type
        Parameters:
        expectedType - The expected return type of the invocation
        args - args to pass to the function in order
        Returns:
        a value of the given type
        Throws:
        JepException - if an error occurs
      • call

        public java.lang.Object call​(java.util.Map<java.lang.String,​java.lang.Object> kwargs)
                              throws JepException
        Invokes this callable with keyword args.
        Parameters:
        kwargs - a Map of keyword args
        Returns:
        an Object value
        Throws:
        JepException - if an error occurs
      • callAs

        public <T> T callAs​(java.lang.Class<T> expectedType,
                            java.util.Map<java.lang.String,​java.lang.Object> kwargs)
                     throws JepException
        Invokes this callable with keyword args, converting the return value to the given class.
        Type Parameters:
        T - the generic type of the return type
        Parameters:
        expectedType - The expected return type of the invocation
        kwargs - a Map of keyword args
        Returns:
        a value of the given type
        Throws:
        JepException - if an error occurs
      • call

        public java.lang.Object call​(java.lang.Object[] args,
                                     java.util.Map<java.lang.String,​java.lang.Object> kwargs)
                              throws JepException
        Invokes this callable with positional args and keyword args.
        Parameters:
        args - args to pass to the function in order
        kwargs - a Map of keyword args
        Returns:
        an Object value
        Throws:
        JepException - if an error occurs
      • callAs

        public <T> T callAs​(java.lang.Class<T> expectedType,
                            java.lang.Object[] args,
                            java.util.Map<java.lang.String,​java.lang.Object> kwargs)
                     throws JepException
        Invokes this callable with positional args and keyword args, converting the return value to the given class.
        Type Parameters:
        T - the generic type of the return type
        Parameters:
        expectedType - The expected return type of the invocation
        args - args to pass to the function in order
        kwargs - a Map of keyword args
        Returns:
        a value of the given type
        Throws:
        JepException - if an error occurs