Class or Dictionary?

Martin De Kauwe mdekauwe at
Fri Feb 11 18:45:16 EST 2011


yes I read a .INI file using ConfigParser, just similar sections (in
my opinion) to make one object which i can then pass to different
classes. E.G.

class Dict2Obj:
    """ Turn a dictionary into an object.

    The only purpose of this is that I think it is neater to reference
    x.soiltemp rather than x['soiltemp'] ;P
    def __init__(self, dict):

        for k, v in dict.items():
            setattr(self, k, v)

class Parser(object):
    """ Load up all the initialisation data.

    Return the met data as an array, perhaps we should really also
return a
    header as well? Return the ini file as various objects.

    def __init__(self, ini_fname, met_fname):

        self.ini_fname = ini_fname
        self.met_fname = met_fname

    def load_files(self):

        # read the driving data in, this may get more complicated?
        met_data = met_data = np.loadtxt(self.met_fname, comments='#')

        # read the configuration file into a different dicts, break up
        # into model_params, control and state
        config = ConfigParser.RawConfigParser()

        # params
        model_params = {}
        sections =
        for section in sections:
            for option in config.options(section):
                model_params[option] = config.getfloat(section,

        # turn dict into an object
        model_params = Dict2Obj(model_params)

        # control
        control = {}
        for option in config.options('control'):
            control[option] = config.getint('control', option)

        # turn dict into an object
        control = Dict2Obj(control)

        initial_state = {}
        sections = ['cinit','ninit','vinit']
        for section in sections:
            for option in config.options(section):
                initial_state[option] = config.getfloat(section,

        # turn dict into objects
        initial_state = Dict2Obj(initial_state)

        return (initial_state, control, model_params,

So I would then pass these objects through my code, for example a
given class would just expect to inherit params perhaps.

class calc_weight(object):

    def __init__(self, params):
        self.params = params

There are also "states" that evolve through the code which various
methods of a given class change. For example lets call it elephants
weight. So the model will do various things to predict changes in our
elephants weight, so there will be a class to calculate his/her food
intake etc. Using this example the other thing I wanted to do was pass
"states" around like self.elephant_weight. However it occurred to me
if I just used self.elephant_weight for example it would make it
harder to call individual components seperately, so better to stick to
the same method and using an object i reasoned. Hence I started with
my emptydict thing, I hope that helps make it clearer?

class EmptyObject:

# generate some objects to store pools and fluxes...
self.pools = EmptyObject()

# add stuff to it
self.pools.elephant_weight = 12.0

thanks for all of the suggestions it is very insightful

More information about the Python-list mailing list