how to make a lot of parameters available to the entire system?

I have objects from various classes that work together to perform a certain task. The task requires a lot of parameters, provided by the user (through a configuration file). The parameters are used deep inside the system.

I have a choice of having the controller object read the configuration file, and then allocate the parameters as appropriate to the next layer of objects, and so on in each layer. But the only objects themselves know which parameters they need, so the controller object would need to learn a lot of detail about every other object.

The other choice is to bundle all the parameters into a collection, and pass the whole collection into every function call (equivalently, create a global object that stores them, and is accessible to everyone). This looks and feels ugly, and would cause a variety of minor technical issues (e.g., I can't allow two objects to use parameters with the same name; etc.)

What to do?

-------------Problems Reply------------

I have used the "global collection" alternative in the past.

If you are concerned with naming: how would you handle this in your config file? The way I see it, your global collection is a datastructure representing the same information you have in your config file, so if you have a way of resolving or avoiding name clashes in your cfg-file, you can do the same in your global collection.

I hope you don't feel like I'm thread-jacking you - what you're asking about is similar to what I was thinking about in terms of property aggregation to avoid the models you want to avoid.

I also nicked a bit of the declarative vibe that Elixir has turned me onto.

I'd be curious what the Python gurus of stack overflow think of it and what better alternatives there might be. I don't like big kwargs and if I can avoid big constructors I prefer to.

#!/usr/bin/python

import inspect
from itertools import chain, ifilter
from pprint import pprint
from abc import ABCMeta

class Property(object):

def __init__(self, value=None):
self._x = value

def __repr__(self):
return str(self._x)

def getx(self):
return self._x

def setx(self, value):
self._x = value

def delx(self):
del self._x

value = property(getx, setx, delx, "I'm the property.")

class BaseClass(object):

unique_baseclass_thing = Property()

def get_prop_tree(self):
mro = self.__class__.__mro__
r = []
for i in xrange( 0, len(mro) - 1 ):

child_prop_names = set(dir(mro[i]))
parent_prop_names = set(dir(mro[i+1]))

l_k = list( chain( child_prop_names - parent_prop_names ) )
l_n = [ (x, getattr(mro[i],x,None)) for x in l_k ]
l_p = list( ifilter(lambda y: y[1].__class__ == Property, l_n))

r.append(
(mro[i],
(dict
( l_p )
)
)
)
return r

def get_prop_list(self):
return list( chain(* [ x[1].items() for x in reversed( self.get_prop_tree() ) ] ) )

class SubClass(BaseClass):
unique_subclass_thing = Property(1)

class SubSubClass(SubClass):
unique_subsubclass_thing_one = Property("blah")
unique_subsubclass_thing_two = Property("foo")

if __name__ == '__main__':

a = SubSubClass()

for b in a.get_prop_tree():
print '---------------'
print b[0].__name__
for prop in b[1].keys():
print "\t", prop, "=", b[1][prop].value
print

for prop in a.get_prop_list():

When you run it..


SubSubClass
unique_subsubclass_thing_one = blah
unique_subsubclass_thing_two = foo

---------------
SubClass
unique_subclass_thing = 1

---------------
BaseClass
unique_baseclass_thing = None

unique_baseclass_thing None
unique_subclass_thing 1
unique_subsubclass_thing_one blah
unique_subsubclass_thing_two foo

Category:python Views:0 Time:2012-01-18

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.172 (s). 11 q(s)