Varghjärta wrote:
...

But there is something that keeps bugging me, and that keeps me from
embracing Python even more as a serious alternative to C#(for me). In
C# I make heavy use of Get & Set, mainly to fire an event that some
property has changed so that one can act on that _if one would need
to. I've grown very used to doing it and it feels like the best way to
make very OO and reusuable and easy to use Classes.
It's fairly straightforward to implement observability for any of the various "modeling" descriptor systems out there. Attached you'll find a quick demo of how one could use BasicProperty with PyDispatcher to produce the effect. The biggest problem is that normally the particular events you want to fire are application specific. Heck, it's a toss-up whether the property or the client should be the sender of a message (basically depends on whether you normally want to filter by one or the other).

The OpenGLContext vrml scenegraph has PyDispatcher-sending fields built-in if you want to see a real-world use. (We have others which use BasicProperty + PyDispatcher, but they're not Open-Source).

Have fun,
Mike

--
________________________________________________
 Mike C. Fletcher
 Designer, VR Plumber, Coder
 http://www.vrplumber.com
 http://blog.vrplumber.com

"""Demonstration of observable properties"""
from dispatch import dispatcher 
from basicproperty import basic

class Observable( basic.BasicProperty ):
        """Sample of creating an observable BasicProperty"""
        SETTING_SIGNAL = "basicproperty.observable.set"
        def __set__( self, client, value ):
                """Override to send dispatcher messages on setting"""
                value = super( Observable, self ).__set__( client, value )
                try:
                        dispatcher.send(
                                signal = self.SETTING_SIGNAL,
                                sender = self,
                                value = value,
                                client = client,
                        )
                except Exception, err:
                        # you'd actually want to log here...
                        pass
                return value

if __name__ == "__main__":
        from basicproperty import propertied
        import random
        class Model( propertied.Propertied ):
                name = Observable(
                        "name", """The name of the object, for some purpose""",
                        defaultValue = "Uncle Tim",
                )
                age = Observable(
                        "age", """How old the object is, often just a guess""",
                        defaultFunction = lambda prop, client: random.randint( 
1, 100),
                )
        
        def onName( sender, client, value ):
                """Process a change to a Model Object"""
                print 'Name', sender, client, value 
        def onAge( client, value ):
                """Process a change to a Model Object"""
                print 'Age', client, value 
        dispatcher.connect( 
                onName,
                sender = Model.name,
                signal = Observable.SETTING_SIGNAL,
        )
        dispatcher.connect( 
                onAge,
                sender = Model.age,
                signal = Observable.SETTING_SIGNAL,
        )
        
        m = Model()
        m.name = 'Peters'
        m.age # note we send a message, as the default is __set__ on retrieval
        m.age = 18
        
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to