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.
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])
box = shapes()
box.square = 10
box.blue = 5
print box.square
print box.blue
print box.green
box.purple = 'violet'
print box.purple
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.