classes vs dicts

Dave Brueck dave at pythonapocrypha.com
Thu May 13 12:33:09 EDT 2004


David wrote:
> My problem is similar, with the additional requirement that I need a
> convenient way to access data deep within a hierarchy of parameters.
> I chose classes over dictionaries because the syntax to access items
> in a deeply nested dictionary is awkward.
>
> dict[window1][plot2][xaxis][label][font][size] = 12
>
> vs
>
> statefiles.window1.plot2.xaxis.label.font.size = 12
>
> The problem is I can't easily save the whole hierarchy to disk.
> Pickle doesn't work with classes.  Also, I worry about the overhead of
> classes when I am just needing a simple container.  A typical
> statefile will have 1000 parameters in 300 classes nested up to ten
> levels deep.  As a structure of nested classes this takes about 74KB
> on disk.  Importing the file creates a .pyc file that is 157KB !!  It
> does seem to import quickly, however, so speed may not be a problem.
>
> Seems like Python could use a "container" structure which would be
> like a class, but without the overhead and with the ability to
> "pickle" the whole structure.

Forgive me if I don't understand the problem, but why can't you just do:

class Bag:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

Then if your data is like:

settings
    network
        ip = '10.20.30.40'
        port = 1234
    logging
        level = 3

you would construct it by hand like:

settings = Bag(network=Bag(ip='10.20.30.40', port=1234), logging=Bag(level=3))

and you can access members like:

print settings.network.ip
or
settings.network.useSSL = 1
etc.

and you can easily pickle _instances_ so persistence isn't really a problem:

from cPickle import dumps, loads
q = dumps(settings)
newSettings = loads(q)

Unless I'm misunderstanding the problem, the only remaining issue is the
overhead. If that really is a problem (most likely not, but maybe if you're
transferring it over a slow network connection or something), then simply use
the built-in gzip or bz2 modules.

-Dave





More information about the Python-list mailing list