Discussion:
[Jython-users] Passing Dictionaries to Java
Byron
2007-10-18 14:00:54 UTC
Permalink
I'm trying to pass a Jython dictionary of the form {"a":"b"} to a Java
method that takes a java.util.Map as its parameter type. The error I
get is:

1st arg can't be coerced to java.util.Map

Is there a technique for doing this? Would it be a worthwhile change
to have org.apache.jython.core.PyDictionary implement the
java.util.Map interface to make this sort of thing more seamless?
David Huebel
2007-10-18 18:04:07 UTC
Permalink
Post by Byron
I'm trying to pass a Jython dictionary of the form {"a":"b"} to a Java
method that takes a java.util.Map as its parameter type. The error I
1st arg can't be coerced to java.util.Map
Is there a technique for doing this? Would it be a worthwhile change
to have org.apache.jython.core.PyDictionary implement the
java.util.Map interface to make this sort of thing more seamless?
I second the suggestion. For me, it would be very handy if dicts and
java.util.Maps were interchangeable. I mix Java and Python pretty
freely, and just today I ran into an error because I passed a Map into
a Python method that tried to call items() on it. The method needed
to accept dicts, too, and writing a special case for each type would
have been ugly. It turned out to be only a minor annoyance because I
was able to for-loop over the map itself, which works on both types,
but in previous cases I have ended up replacing many uses of dicts
with HashMaps in my Python code, just to avoid writing duplicate code
on the Python side. (This works fine for me, but it would obviously
not be an option if I were worried about using my Python code with
CPython.)

Bottom line:

1. When I program in Java, a java.util.Map is a Map is a Map. No worries.
2. When I program in Python, a dict is a dict is a dict. No worries.
3. When I mix Java and Python, I have to pay careful attention to
avoid mixing up maps of different types.

Making case 3 as easy as cases 1 and 2 would be a very nice enhancement.

-David
Moore, Greg
2007-10-18 18:44:22 UTC
Permalink
Sent: Thursday, October 18, 2007 11:04 AM
Subject: Re: [Jython-users] Passing Dictionaries to Java
[snip}
Post by Byron
Is there a technique for doing this? Would it be a worthwhile change
to have org.apache.jython.core.PyDictionary implement the
java.util.Map interface to make this sort of thing more seamless?
[hack]
1. When I program in Java, a java.util.Map is a Map is a Map. No worries.
2. When I program in Python, a dict is a dict is a dict. No worries.
3. When I mix Java and Python, I have to pay careful attention to
avoid mixing up maps of different types.
Making case 3 as easy as cases 1 and 2 would be a very nice
enhancement.
[slash]

[Greg Moore blathered on about: ]

I would agree with David and Byron. I've been bit by this myself. Having
Dictionaries as transparent as strings and lists would be very nice
indeed.

So, can we file a JEP or put something in the source forge tracker to
request this? Is there a process for submitting such a thing?

Greg.


This message and any attachments are intended only for the use of the addressee and may contain information that is privileged and confidential. If the reader of the message is not the intended recipient or an authorized representative of the intended recipient, you are hereby notified that any dissemination of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by e-mail and delete the message and any attachments from your system.
Hasan Diwan
2007-10-18 19:31:56 UTC
Permalink
Post by Byron
Would it be a worthwhile
change
Post by Byron
to have org.apache.jython.core.PyDictionary implement the
java.util.Map interface to make this sort of thing more seamless?
I'd completed a preliminary implementation of java.util.Map for
org.python.core.PyDictionary for the exact reason you specified a few
months back. The only thing missing is that I haven't figured out how
to implement the entrySet() [1] method. I'll put up a blogpost with
the code contained therein tonight at
http://blog.prolificprogrammer.com -- please leave a comment there as
I get too much email and may miss yours.
--
Cheers,
Hasan Diwan <***@gmail.com>
1. http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.html#entrySet()
Hasan Diwan
2007-10-18 20:32:21 UTC
Permalink
Post by Hasan Diwan
I'll put up a blogpost with
the code contained therein tonight at
http://blog.prolificprogrammer.com -- please leave a comment there as
I get too much email and may miss yours.
http://blog.prolificprogrammer.com/articles/2007/10/18/how-to-use-java-and-python-collections-together
--
Cheers,
Hasan Diwan <***@gmail.com>
Byron
2007-10-19 15:35:30 UTC
Permalink
Post by Hasan Diwan
I'd completed a preliminary implementation of java.util.Map for
org.python.core.PyDictionary for the exact reason you specified a
few months back. The only thing missing is that I haven't figured
out how to implement the entrySet() [1] method. I'll put up a
blogpost with the code contained therein tonight at
http://blog.prolificprogrammer.com -- please leave a comment there
as I get too much email and may miss yours.
Seems like for the entrySet method one could just return a wrapper Set
object around the Set object returned by the underlying Map object of
PyDictionary (currently a Hashtable). The Wrapper interface methods
would convert Py.None objects to null. If your patch gets included
I'll add another patch that provides this functionality.
Byron
2007-10-19 15:51:12 UTC
Permalink
Would it be a worthwhile change to have
org.apache.jython.core.PyDictionary implement the java.util.Map
interface to make this sort of thing more seamless?
On a side note it looks like this functionality is already in trunk.
Hasan Diwan
2007-10-19 18:31:05 UTC
Permalink
Post by Byron
On a side note it looks like this functionality is already in trunk.
Where do you see this? I saw Clark Updike wrote a note Dec. 12, 2003
extending the python dictionary to interface with Java, but nothing
more recent.
--
Cheers,
Hasan Diwan <***@gmail.com>
Byron
2007-10-19 21:46:05 UTC
Permalink
Post by Hasan Diwan
Post by Byron
On a side note it looks like this functionality is already in
trunk.
Where do you see this? I saw Clark Updike wrote a note Dec. 12,
2003 extending the python dictionary to interface with Java, but
nothing more recent.
Sorry, you're right, I was mistaken.

Loading...