multi-property groups?

R

Ron Adam

I was trying to see if I can implement property groups so I can set and
pass arguemts as dictionaries. I think this will simplify interfacing
to multiple objects and funtions that use a lot of keywords as arguments.

But so far, the the following seems like it's not the most efficient way
to do it. So is there a better way? Is there a way to use properties
to do this same thing?

I'd also like a way to override the dictionary methods __getitem__,
__setitem__, and __delitem__. (Or an equivalent)


Cheers,
Ron


class Pobject(object):
""" an objects class that can have property groups """
__props__ = {}
def Properties(self, *args):
dct = {}
for i in args[1:]:
dct.setdefault(i,None)
self.__props__[args[0]]=dct
return dct
def __getattr__(self, name):
for dct in self.__props__:
if name in self.__props__[dct]:
return self.__props__[dct].__getitem__(name)
return self.__dict__[name]
def __setattr__(self, name, value):
for dct in self.__props__:
if name in self.__props__[dct]:
self.__props__[dct].__setitem__(name,value)
return
self.__dict__[name] = value

class shapes(Pobject):
def __init__(self):
self.A = self.Properties('A', 'square', 'triangle', 'cube')
self.B = self.Properties('B', 'red', 'blue', 'green')
def show(self, it):
if it == 'shapes':
self.pp(it, self.A) # pass properties as groups!
elif it == 'colors':
self.pp(it, self.B) # have several property groups!
else:
print "I have no %s.\n" % it
def pp(self, it, obj):
print '%s I have:' % it
for item in obj.keys():
print ' %s = %r' % (item,obj[item])
print

box = shapes()
box.square = 10
box.blue = 5
print box.square
print box.blue
print box.green
box.purple = 'violet'
print box.purple
print
box.show('shapes')
box.show('colors')
box.show('flowers')


10
5
None
violet

shapes I have:
cube = None
square = 10
triangle = None

colors I have:
blue = 5
green = None
red = None

I have no flowers.
 
R

Ron Adam

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 ##
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,348
Members
48,026
Latest member
ArnulfoCat

Latest Threads

Top