multi-property groups?

Ron Adam rrr at
Thu Oct 20 01:52:37 CEST 2005

This is what I like about Python, there's almost always a way to do it.  ;-)

Here's an updated version that I think works, but it could use some review.

Any way to make this better?   Should grouped properties share 
references to objects?


Grouped properties:

This need presented it self while programming Tkinter applications where 
a lot of keywords are used, but I think it would also be good for 
general interface building. The external view is of a single object, 
while the internal mechanism keeps the attributes groups so they can be 
forwarded easily as **kwds.

     class foo(object):
         def __init__(self):
             self.__setprops__(name, item_1, item_2, ... item_n)
             name.__doc__ = 'this is the name group of properties'
             def getter():
             def setter():
             def remover():
             name.__getitem__ = getter
             name.__setitem__ = setter
             name.__delitem__ = remover

The following is as close to getting to as I am able I think.
Only somewhat tested... but it seems to work.

class Pdict(dict):
     # we need this so we can override the methods.
     __doc__ = "a property dictionary"

class Pobject(object):
     """ an object with grouped properties """
     __properties__ = {}
     def __setprops__(self, *args):
         Create a group property
            __setprops__(name, p1, p2, p3, ... pn)

         * all arguments are strings to be made into names.
         dct = Pdict()
         for i in args[1:]:
         self.__properties__[args[0]] = dct
         self.__dict__[args[0]] = dct
     def __getattr__(self, name):
         for dct in self.__properties__:
             if name in self.__properties__[dct]:
                 return self.__properties__[dct].__getitem__(name)
         return self.__dict__[name]
     def __setattr__(self, name, value):
         for dct in self.__properties__:
             if name in self.__properties__[dct]:
         self.__dict__[name] = value

## A simple example to test it. ##

class shapes(Pobject):
     def __init__(self):

         # init the A properties
         self.__setprops__('A', 'square', 'triangle', 'cube', 'circle')
         self.A.__doc__ = 'These are the "A" properties'
         def A_getter(name):
             return -self.A[name]     # just to show it will
         self.A.__getitem__ = A_getter

         # init the B properties
         self.__setprops__('B', 'red', 'blue', 'green', 'orange')
         self.B.__doc__ = 'These are the "B" properties'

     def show(self, it):
         if it == 'shapes':
            self.pprint(self.A)  # pass properties as groups!
         elif it == 'colors':
            self.pprint(self.B)  # have several property groups!
            print "I have no %s.\n" % it

     def pprint(self, obj):
         print obj.__doc__
         for item in obj.keys():
             print '  %s = %r'  % (item,obj[item])

box = shapes()

# Set items; no need to now what group
# they are in.
box.cube = 6
box.square = 4
box.triangle = 3 = 0

# Update a whole group at once with the group name.
box.B.update(blue='blue', green='green', red='rose', orange='orange') = 'red'

# Get items
print box.square

# Show the groups.'shapes')'colors')'flowers')

## outputs ##

These are the "A" properties
   cube = 6
   square = 4
   triangle = 3
   circle = 0

These are the "B" properties
   blue = 'blue'
   green = 'green'
   orange = 'orange'
   red = 'red'

I have no flowers.

## end ##

More information about the Python-list mailing list