Jython interop

197 views
Skip to first unread message

rob

unread,
Jan 24, 2010, 5:26:41 PM1/24/10
to Clojure
Hi,

I was wondering if anyone has tried somehow calling Jython functions
from within Clojure, and how you went about doing this if so. I have
not used Jython, but I would imagine the Jython interpreter can be
invoked in the same way as any other java code, and Python programs
can be run within it. However I wonder if it would be possible to
somehow call individual python functions from Clojure. Like I said, I
have not tried this yet, so it might actually be straightforward and
obvious. I'm just wondering if anyone has tried doing this.

Thanks,
Rob

Eric Lavigne

unread,
Jan 24, 2010, 7:08:03 PM1/24/10
to clo...@googlegroups.com

I haven't tried it, but the strategies for executing Jython code from
Java should work at least as well from Clojure. There are two
approaches.

One approach is to compile the Jython code into a class file. This
requires some work on the Python side (similar to using gen-class in
Clojure so that Clojure code can be called from Java), and the Python
class of interest can't use multiple-inheritance. This approach is
discussed in the section called "How to create Jython classes callable
from Java programs" at
http://www.seanet.com/~bugbee/python/jython4.html

Another approach is to invoke the Jython interpreter as you suggested,
load a Python script, and ask the interpreter to evaluate a function
for you. An example can be found in this page:
http://bytes.com/topic/python/answers/22390-executing-jython-function-java

Additional discussion about the second approach can be found in the
section called "How to run Python programs from Java" at
http://www.seanet.com/~bugbee/python/jython4.html

Joonas Pulakka

unread,
Jan 25, 2010, 6:10:23 AM1/25/10
to Clojure

The procedure I've used most with Jython (and found to be quite
robust) is to define an interface in Java, and then implement (extend)
that interface in Jython. Then just create an instance of that object
with JythonInterpreter, and you get a valid Java object implementing
your interface. Here's some description on that topic:
http://jythonpodcast.hostjava.net/jythonbook/chapter10.html

I haven't actually tried to combine it with Clojure, but with this
"interface approach" it should be a cakewalk, since Clojure calls to
Java are direct with no "magic translation layers". Calling Clojure
from Jython could be a bit trickier, though, since Jython has these
translation layers.

Best Regards,
Joonas

Marc Downie

unread,
Jan 24, 2010, 7:20:10 PM1/24/10
to clo...@googlegroups.com

One approach is to compile the Jython code into a class file. This
requires some work on the Python side (similar to using gen-class in
Clojure so that Clojure code can be called from Java), and the Python
class of interest can't use multiple-inheritance. This approach is
discussed in the section called "How to create Jython classes callable
from Java programs" at
http://www.seanet.com/~bugbee/python/jython4.html


Alas that page is a little out of date and jythonc (Jython's stand alone gen-class thing) is going / gone in the most recent versions of Jython. One thing that I should add is that you can create implementations of Java interfaces that actually do cast correctly Java side:


Which isn't quite "call individual Python functions" but will end up getting less intimate with the Jython runtime than the work that I'm doing.

Marc.

Marc Downie

unread,
Jan 24, 2010, 7:09:44 PM1/24/10
to clo...@googlegroups.com
It's possible. We're doing it a little right now (we're mainly going the other way, calling Clojure from Jython with Clojure embedded in a JSR223-ish environment) — see this thread http://groups.google.com/group/clojure/msg/f878d7a32125ce58?hl=en for a hint.

You end up having to wrap up the guts of Jython's Java interface which is actually fairly small. In our environment, where you can mix languages and where Jython's namespace is automatically propagated into Clojure for you, you have something like:

// python

def banana(x, y):
return "x is %s, y is %s" % (x, y)

// clojure

(defn java2py [x] (into-array PyObject (map (fn [n] (. org.python.core.Py java2py n)) x)))
(defn call-jython [x & y] (.__tojava__ (.__call__ x (java2py y)) Object))

(println (call-jython banana 5 "b"))

Please pardon the Clojure code — I'm a Jython expert, but a total Clojure amateur. Ultimately, given a PyFunction, you need to invoke PyObject __call__(PyObject[] x) on it, thus you need to wrap (Py.java2Py) the arguments and unwrap (.__tojava__) the return value.

Building on this "foundation" we can also instantiate objects and call methods:

// python
class Something:
def callMe(self, x, y):
print "callMe called %s %s " % (x,y)
return 10+x+y

// clojure

(println (call-jython (.__getattr__ (call-jython Something) "callMe") 5 5))

As you can see Jython's classes never appear Java side. So Clojure's java interop is thwarted (you can't (.callMe ...) something)

I'd be fascinated to see somebody who actually knows Clojure throw down that fantastic macro that makes these two kinds of calls pretty. Meanwhile I'm having fun learning my way towards it.

Marc.




rob

unread,
Jan 25, 2010, 11:34:25 AM1/25/10
to Clojure
Thank you for these good ideas! I'm going to try it. My goal is to
use the Natural Language Toolkit in Clojure. I also posted the same
question here: http://stackoverflow.com/questions/2129253/clojure-jython-interop

Rob

On Jan 24, 7:08 pm, Eric Lavigne <lavigne.e...@gmail.com> wrote:
> > I was wondering if anyone has tried somehow calling Jython functions
> > from within Clojure, and how you went about doing this if so.  I have
> > not used Jython, but I would imagine the Jython interpreter can be
> > invoked in the same way as any other java code, and Python programs
> > can be run within it.  However I wonder if it would be possible to
> > somehow call individual python functions from Clojure.  Like I said, I
> > have not tried this yet, so it might actually be straightforward and
> > obvious.  I'm just wondering if anyone has tried doing this.
>
> I haven't tried it, but the strategies for executing Jython code from
> Java should work at least as well from Clojure. There are two
> approaches.
>
> One approach is to compile the Jython code into a class file. This
> requires some work on the Python side (similar to using gen-class in
> Clojure so that Clojure code can be called from Java), and the Python
> class of interest can't use multiple-inheritance. This approach is
> discussed in the section called "How to create Jython classes callable

> from Java programs" athttp://www.seanet.com/~bugbee/python/jython4.html


>
> Another approach is to invoke the Jython interpreter as you suggested,
> load a Python script, and ask the interpreter to evaluate a function

> for you. An example can be found in this page:http://bytes.com/topic/python/answers/22390-executing-jython-function...

Marc Downie

unread,
Jan 25, 2010, 2:41:39 PM1/25/10
to clo...@googlegroups.com
On Mon, Jan 25, 2010 at 5:34 PM, rob <r.p....@gmail.com> wrote:
Thank you for these good ideas!  I'm going to try it.  My goal is to
use the Natural Language Toolkit in Clojure.   I also posted the same
question here: http://stackoverflow.com/questions/2129253/clojure-jython-interop

Since NLTK is variously just bunch functions, or classes without any java interface counterpart (because it's a pure Python library, it doesn't know anything about Java), I'm afraid you really are looking at the kind of things that I wrote in my first email. (The leading answer on stackoverflow seems technically correct, but misses the Py.java2py and .__tojava__ convenience calls, and has a go at to recoding part of them in Clojure).

have fun (and do let me know when you come up with something tighter than what I wrote).

best,

Marc.
 

Rob

On Jan 24, 7:08 pm, Eric Lavigne <lavigne.e...@gmail.com> wrote:
> > I was wondering if anyone has tried somehow calling Jython functions
> > from within Clojure, and how you went about doing this if so.  I have
> > not used Jython, but I would imagine the Jython interpreter can be
> > invoked in the same way as any other java code, and Python programs
> > can be run within it.  However I wonder if it would be possible to
> > somehow call individual python functions from Clojure.  Like I said, I
> > have not tried this yet, so it might actually be straightforward and
> > obvious.  I'm just wondering if anyone has tried doing this.
>
> I haven't tried it, but the strategies for executing Jython code from
> Java should work at least as well from Clojure. There are two
> approaches.
>
> One approach is to compile the Jython code into a class file. This
> requires some work on the Python side (similar to using gen-class in
> Clojure so that Clojure code can be called from Java), and the Python
> class of interest can't use multiple-inheritance. This approach is
> discussed in the section called "How to create Jython classes callable
> from Java programs" athttp://www.seanet.com/~bugbee/python/jython4.html
>
> Another approach is to invoke the Jython interpreter as you suggested,
> load a Python script, and ask the interpreter to evaluate a function
> for you. An example can be found in this page:http://bytes.com/topic/python/answers/22390-executing-jython-function...
>
> Additional discussion about the second approach can be found in the
> section called "How to run Python programs from Java" athttp://www.seanet.com/~bugbee/python/jython4.html

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

rob

unread,
Jan 25, 2010, 6:31:52 PM1/25/10
to Clojure
Thanks again Marc, I made a note of it on the StackOverflow thread.

On Jan 25, 2:41 pm, Marc Downie <m...@openendedgroup.com> wrote:

> > clojure+u...@googlegroups.com<clojure%2Bunsu...@googlegroups.com>

rob

unread,
Mar 7, 2010, 12:49:23 AM3/7/10
to Clojure
Since we had this discussion I've been using Jython in Clojure and
I've been growing a little library to make things easier. I decided
it was useful enough to make live (though probably far from complete,
seeing as my use case has been solely on one python library, nltk).

http://code.google.com/p/clojure-python/source/browse/trunk/
http://code.google.com/p/clojure-python/source/browse/trunk/src/python.clj

Let me know what you think, feedback, suggestions, contributions, etc.

Rob

On Jan 25, 6:31 pm, rob <r.p.l...@gmail.com> wrote:
> Thanks again Marc, I made a note of it on the StackOverflow thread.
>
> On Jan 25, 2:41 pm, Marc Downie <m...@openendedgroup.com> wrote:
>
>
>
> > On Mon, Jan 25, 2010 at 5:34 PM, rob <r.p.l...@gmail.com> wrote:
> > > Thank you for these good ideas!  I'm going to try it.  My goal is to

> > > use the Natural Language Toolkit inClojure.   I also posted the same


> > > question here:
> > >http://stackoverflow.com/questions/2129253/clojure-jython-interop
>
> > Since NLTK is variously just bunch functions, or classes without any java
> > interface counterpart (because it's a pure Python library, it doesn't know
> > anything about Java), I'm afraid you really are looking at the kind of
> > things that I wrote in my first email. (The leading answer on stackoverflow
> > seems technically correct, but misses the Py.java2py and .__tojava__
> > convenience calls, and has a go at to recoding part of them inClojure).
>
> > have fun (and do let me know when you come up with something tighter than
> > what I wrote).
>
> > best,
>
> > Marc.
>
> > > Rob
>
> > > On Jan 24, 7:08 pm, Eric Lavigne <lavigne.e...@gmail.com> wrote:
> > > > > I was wondering if anyone has tried somehow callingJythonfunctions

> > > > > from withinClojure, and how you went about doing this if so.  I have
> > > > > not usedJython, but I would imagine theJythoninterpreter can be


> > > > > invoked in the same way as any other java code, and Python programs
> > > > > can be run within it.  However I wonder if it would be possible to

> > > > > somehow call individual python functions fromClojure.  Like I said, I


> > > > > have not tried this yet, so it might actually be straightforward and
> > > > > obvious.  I'm just wondering if anyone has tried doing this.
>

> > > > I haven't tried it, but the strategies for executingJythoncode from
> > > > Java should work at least as well fromClojure. There are two
> > > > approaches.
>
> > > > One approach is to compile theJythoncode into a class file. This


> > > > requires some work on the Python side (similar to using gen-class in

> > > >Clojureso thatClojurecode can be called from Java), and the Python


> > > > class of interest can't use multiple-inheritance. This approach is

> > > > discussed in the section called "How to createJythonclasses callable


> > > > from Java programs" athttp://www.seanet.com/~bugbee/python/jython4.html
>

> > > > Another approach is to invoke theJythoninterpreter as you suggested,


> > > > load a Python script, and ask the interpreter to evaluate a function
> > > > for you. An example can be found in this page:
> > >http://bytes.com/topic/python/answers/22390-executing-jython-function...
>
> > > > Additional discussion about the second approach can be found in the
> > > > section called "How to run Python programs from Java" athttp://
> > >www.seanet.com/~bugbee/python/jython4.html
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Clojure" group.

> > > To post to this group, send email tocl...@googlegroups.com

Reply all
Reply to author
Forward
0 new messages