Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic invokeMethod #22

Open
Robbie-Palmer opened this issue Oct 25, 2022 · 1 comment
Open

Generic invokeMethod #22

Robbie-Palmer opened this issue Oct 25, 2022 · 1 comment

Comments

@Robbie-Palmer
Copy link
Contributor

It would be great if invokeMethod could take the return type, similar to public <T> T get(String name, Class<T> clazz)

The below code which passes an integer to Python and expects an integer back fails with the error class java.lang.Long cannot be cast to class java.lang.Integer

var pyFnName = "add_one";
var pythonCode = pyFnName + " = lambda x: x + 1";
int input = 5;
try (var interpreter = new PythonInterpreter(config);) {
    interpreter.exec(pythonCode);
    var pyFn = (PyObject) interpreter.get(pyFnName);
    var out = (int) pyFn.invokeMethod("__call__", input);
    System.out.println(out);
}

An integer can be retrieved by using the generic get alongside exec but is fiddlier and less pretty

var pyFnName = "add_one";
var pythonCode = pyFnName + " = lambda x: x + 1";
int input = 5;
try (var interpreter = new PythonInterpreter(config);) {
    interpreter.exec(pythonCode);
    var resultVarName = "result";
    interpreter.exec(resultVarName + "=" + pyFnName + "(" + input + ")");
    var out = (int) interpreter.get(resultVarName, Integer.class);
    System.out.println(out);
}

Or can accept the value back as a Long and convert it to an int value

var out = (Long) pyFn.invokeMethod("__call__", input);
var intOut = out.intValue();

So functionally everything is available, with a public <T> T invokeMethod(String name, Class<T> clazz, Object... args) method just providing syntactic sugar

@HuangXingBo
Copy link
Collaborator

@Robbie-Palmer I thinks it is a good optimization for usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants