is this data structure build-in or I'll have to write my own class?

mkPyVS mikeminer53 at hotmail.com
Wed Feb 20 15:11:59 EST 2008


On Feb 20, 12:07 pm, thebjorn <BjornSteinarFjeldPetter... at gmail.com>
wrote:
> On Feb 20, 3:32 pm, "Jorge Vargas" <jorge.var... at gmail.com> wrote:
>
>
>
> > On Feb 20, 2008 8:15 AM, Larry Bates <larry.ba... at websafe.com> wrote:
>
> > > Jorge Vargas wrote:
> > > > I need a data structure that will let me do:
>
> > > > - attribute access (or index)
> > > > - maintain the order (for iter and print)
> > > > - be mutable.
>
> [...]
>
> > > Sounds like a good time to learn ElementTree (included in Python 2.5 but
> > > available for earlier versions).
>
> > I am using ET, to fetch the data from the XML, after that I want a
> > plain python object. for the rest of the program.
>
> Ok, you just lost me... Why do you thin ET is not appropriate (*)?  It
> fits all your requirements, is optimized for representing hierarchical
> data (especially xml), it is fast, it is well tested, it has a
> community of users, it is included in the standard library, etc., etc.
>
> ...maybe I didn't grok what you meant by "plain python object"?
>
> -- bjorn
>
> (*) I just had a flashback to the movie ET -- the scene when he's in
> the closet ;-)

This isn't so optimal but I think accomplishes what you desire to some
extent... I *think* there is some hidden gem in inheriting from dict
or an mapping type that is cleaner than what I've shown below though.

class dum_struct:
   def __init__(self,keyList,valList):
      self.__orderedKeys = keyList
      self.__orderedValList = valList
   def __getattr__(self,name):
      return self.__orderedValList[self.__orderedKeys.index(name)]


keys = ['foo','baz']
vals = ['bar','bal']

m = dum_struct(keys,vals)

print m.foo



More information about the Python-list mailing list