multi-property groups?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Adam

    #1

    multi-property groups?



    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__(sel f, name):
    for dct in self.__props__:
    if name in self.__props__[dct]:
    return self.__props__[dct].__getitem__(na me)
    return self.__dict__[name]
    def __setattr__(sel f, name, value):
    for dct in self.__props__:
    if name in self.__props__[dct]:
    self.__props__[dct].__setitem__(na me,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('shape s')
    box.show('color s')
    box.show('flowe rs')


    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.


  • Ron Adam

    #2
    Re: multi-property groups?


    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__(se lf, *args):
    """
    Create a group property
    __setprops__(na me, 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.__properti es__[args[0]] = dct
    self.__dict__[args[0]] = dct
    def __getattr__(sel f, name):
    for dct in self.__properti es__:
    if name in self.__properti es__[dct]:
    return self.__properti es__[dct].__getitem__(na me)
    return self.__dict__[name]
    def __setattr__(sel f, name, value):
    for dct in self.__properti es__:
    if name in self.__properti es__[dct]:
    self.__properti es__[dct].__setitem__(na me,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.__getite m__ = 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(sel f.A) # pass properties as groups!
    elif it == 'colors':
    self.pprint(sel f.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(bl ue='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('shape s')
    box.show('color s')
    box.show('flowe rs')



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

    Comment

    Working...