From: David H. <dav...@gm...> - 2008-04-23 15:28:32
|
On Wed, Apr 23, 2008 at 7:06 AM, Heshan Suriyaarachchi <hes...@gm...> wrote: > Hi, > I am using java.util.map to store some data ( one key and two value > pairs , per row ) and return it to a python function . What kind of a data > structre should I use to catch it , inside python . If you return a java.util.Map, your Python code will get a java.util.Map, and you can operate on it just like you would in Java. You can call containsKey, containsValue, entrySet, get, put, and all the other Map operations. Jython also lets you use Python syntax for lookup and iteration on a Map: >>> from java.util import HashMap >>> h1 = HashMap() >>> h1['foo'] = 'bar' >>> h1['foo'] 'bar' >>> 'foo' in h1 1 >>> h1.containsKey('foo') 1 >>> for k in h1: ... print '%s -> %s' % (k, h1[k]) ... foo -> bar >>> However, looking up a key that isn't there results in different behavior in Maps and dicts: >>> h1['baz'] >>> h2 = {} >>> h2['baz'] Traceback (innermost last): File "<console>", line 1, in ? KeyError: baz >>> The Map returns a null value, but the dict throws an exception. So, unfortunately, most real Python code that is written for a dict will not work for a Map, and vice-versa. It may appear to work if you don't store null values and don't do any lookups that fail, but at the very least the error-checking will be broken. But everything will be okay if you always know which type you're dealing with, Map or dict. - David |