there are many variations and combined functionality that we can do
with these dicts with attribute like functionality... below is an
example of one that lets you do the dot thing on copies of a dict
(comes in vary handy at times) and uses cPickle (can send usage if
unclear)

something else very useful, is the ability to take an xml doc and
convert that to these dicts, makes handling xml and dict seamless. can
convert from xml to dict like object, or normal dict types, or any of
these combinations. WOuld have posted it, but its a little big and I
don;t seem to understand how to add an attachement to these posts. But
will gladly email if anyone is interested.

Mart

import cPickle
class blueDict(dict):
    def __init__(self,data=None):
        if data:
            dict.__init__(self, data)
        else:
            dict.__init__(self)
        dic=self.__dict__
        dic['__ver__']='0.1'

    def __setattr__(self,name,val):
        if name in self.__dict__:
            self.__dict__[name]= val
        else:
            self[name]=val

    def __getattr__(self,name):
        if name in self.__dict__:
            return self.__dict__[name]
        else:
            return self[name]

    def setDict(self,name,val):
        self.__dict__[name]=val
        return self

    def getDict(self):
        return self.__dict__

    def setItem(self,name,val):
        self[name]=val
        return self

    def __getstate__(self):
        return self.__dict__.copy()

    def __setstate__(self,dict):
        self.__dict__.update(dict)

    def copy(self):
        return cPickle.loads(cPickle.dumps(self))


On Jun 13, 7:57 am, Ross Peoples <ross.peop...@gmail.com> wrote:
> I make heavy use of Storage in my apps because dot notation is so much
> easier and faster. It is completely backwards compatible with Python's
> dictionary. I usually end up putting this at the top of all my controllers
> and modules:
>
> from gluon.storage import Storage
>
> Then instead of using a dictionary:
>
> d = {'a': 1}
> d['b'] = 2
> return d['a']
>
> Use Storage:
>
> d = Storage({'a': 1})
> d.b = 2
> return d.a

Reply via email to