From: James C. <ji...@mi...> - 2002-05-28 16:39:10
|
I have a different kind of approach that might be interesting... if you have a Java class that either is a or has a Hashtable -like (could be HapMap, etc) dictionary You can create a Python subclass that accesses it using the access methods: For instance the java java.util.Properties class has a hashtable, and get and set methods... to make it pythonic: class PyProperties(java.util.Properties): =20 def __getattr__(self, name): return self.getProperty(name) =20 def __setattr__(self, name, value): self.setProperty(name, str(value)) return value Then in Jython: >>> p =3D PyProperties() >>> p['ten'] =3D 'eleven' >>> p['ten'] eleven =20 The one thing I haven't tried, but will try soon is to implement methods in Java that have the names __getattr__ and __setattr__ so that there is no python sub-classing necessary. =20 -Jim > -----Original Message----- > From: Dinu Gherman [mailto:gh...@da...]=20 > Sent: Tuesday, May 28, 2002 12:22 PM > To: jyt...@li... > Subject: Re: [Jython-users] Getting Python-like dictionaries? >=20 >=20 > Pedroni <pe...@in...>: >=20 > > in your case the more interesting part is [...] > > or more precisely whether > >=20 > > import java.util > > isinstance(dict,java.lang.Dictionary) or=20 > instance(dict,java.lang.Map) > >=20 > > is true, in that case Jython does the work for you and > > you can use dict[...], dict[...] =3D (see JythonEss p. 117) > >=20 > > otherwise you should write a wrapper yourself. >=20 >=20 > Yep, it turns out the following code gets the job done (nasty=20 > imports omitted ;-): >=20 > def convertNSDictionaryToPyDict(nsDict): > "Convert an NSDictionary to a Jython Dictionary."=20 > =20 > myPool =3D NSAutoreleasePool.push() > =20 > dict =3D {} > keyEnum =3D nsDict.keyEnumerator() > while keyEnum.hasMoreElements(): > k =3D keyEnum.nextElement() > dict[k] =3D nsDict.objectForKey(k) > =20 > NSAutoreleasePool.pop(myPool) > =20 > return dict=20 >=20 > Thanks, Samuele and Danny! >=20 > Dinu >=20 > _______________________________________________________________ >=20 > Don't miss the 2002 Sprint PCS Application Developer's=20 > Conference August 25-28 in Las Vegas --=20 > http://devcon.sprintpcs.com/adp/index.cfm >=20 >=20 > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users >=20 |