multi-property groups?

Ron Adam rrr at ronadam.com
Wed Oct 19 19:52:37 EDT 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?

Cheers,
    Ron


"""
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:]:
             dct.setdefault(i,None)
         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.__properties__[dct].__setitem__(name,value)
                 return
         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!
         else:
            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])
         print

box = shapes()

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

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

# Get items
print box.square
print box.blue
print box.green

# Show the groups.
box.show('shapes')
box.show('colors')
box.show('flowers')



## outputs ##

-4
blue
green
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