Passing a Python Object/Function to Java and call

706 views
Skip to first unread message

guil...@intellisense.io

unread,
Jul 9, 2018, 1:15:20 PM7/9/18
to Jep Project
Hi,

I want to be able to pass a function or an object (with a run() method for instance) from Python to Java. I would create the object in Python, pass to java and execute in Java at a later time.

I tried having a java method with a PyObject as a parameter and then calling that from Python and passing the object, afterwards I tried  the method but no success:

import io.undertow.Undertow;
import jep.python.PyObject;

public class Server {

public void run(PyObject handler, Integer port, String url) {
Undertow server = Undertow.builder()
.addHttpListener(port, url)
.setHandler(new HandlerManager().buildHandler(handler)).build();
server.start();
}
}

Python Code

import datetime

class Handler:
def __init__(self, name):
self.name = name

def run(self):
return self.name + ": Test from Python - " + datetime.datetime.now().isoformat() + "\n"

Server().run(Handler("test"), 8080, "localhost")

This is the exception I get:

jep.JepException: <class 'TypeError'>: Error converting parameter 1: Expected jep.python.PyObject but received a Sample.

Any thoughts?

Nathan Jensen

unread,
Jul 9, 2018, 2:43:58 PM7/9/18
to Jep Project
Passing around PyObjects is not supported well on Jep 3.7.  Using PyObjects in Java was significantly improved in 3.8.  Please try with the 3.8 release candidate and report back on what happens.  https://github.com/ninia/jep/tree/v3.8rc

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/15d973e0-41c6-47b8-8866-8f3eea4cb58c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

guil...@intellisense.io

unread,
Jul 10, 2018, 5:43:18 AM7/10/18
to Jep Project
I installed Jep 3.8 as you suggested, now it seems I have a proper PyObject:


I'm still not sure how to call a method having the PyObject
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

guil...@intellisense.io

unread,
Jul 10, 2018, 5:55:40 AM7/10/18
to Jep Project
Getting the function with getAttr as a PyCallable and then calling and getting the result as string made it work:

try {
PyCallable callable = (PyCallable) handler.getAttr("run");
String result = callable.call().toString();
System.out.println(result);
} catch (JepException e) {
System.out.println(e);
}
Reply all
Reply to author
Forward
0 new messages