|
From: <bc...@wo...> - 2000-11-28 11:13:53
|
On Mon, 27 Nov 2000 18:07:45 -0800, you wrote: >All, I would like to use PyLists and PyDictionaries interchangably between >Java and Jython. Me too. So would JimH a long time ago (item #2): http://www.python.org/pipermail/jpython-interest/1998-October/003270.html The seconds half of this item is already in place, but the first "This will both allow JPython collections to be easily used from Java" is not. >I thought of subclassing PyDictionary and implementing the >Map interface, but there's a name collision with the "values()" method (as >PyDictionary returns a PyList. Friggin' Java and it's lack of return type >overloading.) > >Needless to say, I thought of one other way, modify the PyList and >PyDictionary classes so that they implement the above-mentioned interfaces. >Given that they use Java classes under the hood that do support these >interfaces, these methods would simply be a pass-through to the protected >object's methods by the same name. > >Additionally, given that PyLists would (indirectly) implement the Collection >interface, the values() method would make both Jython and Java happy. It is not that simple, but it can be done. Python must use a different version of values() than the one java is seeing. That can be done in PyDictionary.classDictInit where the "values" entry already are exchanged with a high performence version. The real problem with this approach is java1 compatibility. It is an unmoving requirement that jython can be used in a java1 browser like IE. So all uses of java2 features must be wrapped in a separate class. See CollectionProxy2.java and Java2Accessibility.java for example on how this can be done. A possible alternative to implementing Map & Collection, could be to extend the __tojava__() behaviour of PyList and PyDictionary. For PyDictionary: public Object __tojava__(Class c) { if (java.util.Map.class.isAssignableFrom(c)) return table; return super.__tojava__(c); } but where the actual test is moved into a separate and dynamicly loaded class. Things get trickier for PyList & PyStringMap because they doesn't already have a class that implements Collection or Map. If we want to maintain the mutability of these collections, the returned object from __tojava__ must be able to modify the original collection. If we make such changes we can then pass python lists and dicts to java methods: from java.util import LinkedList, HashMap print LinkedList([1,2,3,4]) print HashMap({ 'a':1, 'b':2 }) but I'm not sure that is what most people will expect. Take a look at the utility functions from "Thinking in Patterns with Java" http://www.mindview.net/TIPatterns/ There is a method that converts a dict to a map: public static Map toMap(PythonInterpreter interp, String pyName){ PyList pa = ((PyDictionary)interp.get(pyName)).items(); Map map = new HashMap(); while(pa.__len__() != 0) { PyTuple po = (PyTuple)pa.pop(); Object first = po.__finditem__(0).__tojava__(Object.class); Object second = po.__finditem__(1).__tojava__(Object.class); map.put(first, second); } return map; } The interesting thing is the __tojava__(Object.class) on both key and values. That will unwrap the python wrapper and make the Map must more usefull in java IMO. But that will break all attempts to maintain mutability. Any modification that is done on the java Map or List does no longer show up in python dict or list. All in all, I don't think it is worth it to add direct support for java2 collections to python. I would rather see a utility module which had the necessary functions to convert python collection into java collections: import jcollection print jcollection.LinkedList([1,2,3,4]) print jcollection.HashMap({ 'a':1, 'b':2 }) This conversion should then be done recursively. >I can probably perform these changes and send a patch set out via >e-mail...Interested? > >Also, one other thing of note: I've been trying to execute CGI scripts >under the Servlet architecture without having to make code mods to the >Python script (or at least making minimal changes.) As such, I've >implemented a cgi.FieldStorage class in Java that acts like the CPython >equivalent, as well as a Java class equivalent for Cookie.SimpleCookie. Couldn't these classes also be created as python modules? >They're surely not fully-fleshed out and may have bugs but I would be quite >happy to send the source out via e-mail or post them to an appropriate site. >It might be worthwhile to include them as standard libraries once the bugs >are all worked out and they're fully compatable with the CPython >equivalents. regards, finn |