-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into cleanup-deephaven-p…
…ython-dependencies
- Loading branch information
Showing
99 changed files
with
1,942 additions
and
2,334 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Integrations/src/main/java/io/deephaven/integrations/python/PythonBiFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (c) 2016-2022 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.BiFunction; | ||
import java.util.function.BinaryOperator; | ||
|
||
import static io.deephaven.integrations.python.PythonUtils.pyApplyFunc; | ||
|
||
/** | ||
* A {@link BiFunction} implementation which calls a Python callable. | ||
* | ||
* @param <T> input argument class | ||
* @param <U> input argument class | ||
*/ | ||
@SuppressWarnings("unused") | ||
@ScriptApi | ||
public class PythonBiFunction<T, U, R> implements BiFunction<T, U, R> { | ||
private final PyObject pyCallable; | ||
private final Class<R> classOut; | ||
|
||
/** | ||
* Creates a {@link BiFunction} 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, Object)} method. | ||
* This should be the result of converting or unwrapping the output of {@code pyCallable}. | ||
*/ | ||
public PythonBiFunction(final PyObject pyCallable, final Class<R> classOut) { | ||
this.pyCallable = pyApplyFunc(pyCallable); | ||
this.classOut = classOut; | ||
} | ||
|
||
@Override | ||
public R apply(T t, U u) { | ||
PyObject wrapped = PythonObjectWrapper.wrap(t); | ||
PyObject wrappedOther = PythonObjectWrapper.wrap(u); | ||
PyObject out = pyCallable.call("__call__", wrapped, wrappedOther); | ||
return PythonValueGetter.getValue(out, classOut); | ||
} | ||
|
||
|
||
public static class PythonBinaryOperator<T> extends PythonBiFunction<T, T, T> implements BinaryOperator<T> { | ||
/** | ||
* Creates a {@link PythonBinaryOperator} 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, Object)} method. | ||
* This should be the result of converting or unwrapping the output of {@code pyCallable}. | ||
*/ | ||
public PythonBinaryOperator(PyObject pyCallable, Class<T> classOut) { | ||
super(pyCallable, classOut); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
Integrations/src/main/java/io/deephaven/integrations/python/PythonObjectWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending | ||
*/ | ||
package io.deephaven.integrations.python; | ||
|
||
import org.jpy.PyModule; | ||
import org.jpy.PyObject; | ||
|
||
/** | ||
* This is a helper class for wrapping a Java object in a Python wrapper and unwrapping a Python wrapper to a raw Java | ||
* object. | ||
*/ | ||
public class PythonObjectWrapper { | ||
private static final PyModule PY_WRAPPER_MODULE = PyModule.importModule("deephaven._wrapper"); | ||
|
||
/** | ||
* Unwrap a Python object to return the wrapped Java object. | ||
* | ||
* @param t An instance of {@link PyObject}. | ||
* @return The wrapped Java object. | ||
*/ | ||
public static Object unwrap(PyObject t) { | ||
// noinspection ConstantConditions | ||
return PY_WRAPPER_MODULE.call("unwrap", t).getObjectValue(); | ||
} | ||
|
||
/** | ||
* Wrap a raw Java object with a Python wrapper if one exists for its type, otherwise return a JPY mapped Python | ||
* object. | ||
* | ||
* @param t A Java object. | ||
* @return A {@link PyObject} instance representing the Python wrapper object. | ||
*/ | ||
public static PyObject wrap(Object t) { | ||
// noinspection ConstantConditions | ||
return PY_WRAPPER_MODULE.call("wrap_j_object", t); | ||
} | ||
} |
Oops, something went wrong.