All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.deephaven.integrations.python.PythonFunction Maven / Gradle / Ivy

//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.integrations.python;

import io.deephaven.util.annotations.ScriptApi;
import org.jpy.PyObject;

import java.util.function.Function;
import java.util.function.UnaryOperator;

import static io.deephaven.integrations.python.PythonUtils.pyApplyFunc;

/**
 * A {@link Function} implementation which calls a Python callable.
 * 
 * @param  input argument class
 */
@SuppressWarnings("unused")
@ScriptApi
public class PythonFunction implements Function {
    private final PyObject pyCallable;
    private final Class classOut;

    /**
     * Creates a {@link Function} which calls a Python function.
     *
     * @param pyCallable The python object providing the function - must either be callable or have an {@code apply}
     *        attribute which is callable.
     * @param classOut The specific Java class expected to be returned by the {@link #apply(Object)} method. This should
     *        be the result of converting or unwrapping the output of {@code pyCallable}.
     * 
     */
    public PythonFunction(final PyObject pyCallable, final Class classOut) {

        this.pyCallable = pyApplyFunc(pyCallable);
        this.classOut = classOut;

    }

    @Override
    public R apply(T t) {
        PyObject wrapped = PythonObjectWrapper.wrap(t);
        PyObject out = pyCallable.call("__call__", wrapped);
        return PythonValueGetter.getValue(out, classOut);
    }

    public static class PythonUnaryOperator extends PythonFunction implements UnaryOperator {

        /**
         * Creates a {@link UnaryOperator} which calls a Python function.
         *
         * @param pyCallable The python object providing the function - must either be callable or have an {@code apply}
         *        attribute which is callable.
         * @param classOut The specific Java class expected to be returned by the {@link #apply(Object)} method. This
         *        should be the result of converting or unwrapping the output of {@code pyCallable}.
         */
        public PythonUnaryOperator(PyObject pyCallable, Class classOut) {
            super(pyCallable, classOut);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy