Popular recipes tagged "observer"http://code.activestate.com/recipes/tags/observer/2013-03-09T10:03:10-08:00ActiveState Code RecipesObserver Pattern (Python) 2013-03-09T10:03:10-08:00Mauro B. Bianchttp://code.activestate.com/recipes/users/4185493/http://code.activestate.com/recipes/578484-observer-pattern/ <p style="color: grey"> Python recipe 578484 by <a href="/recipes/users/4185493/">Mauro B. Bianc</a> (<a href="/recipes/tags/cascade/">cascade</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/patterns/">patterns</a>, <a href="/recipes/tags/setattr/">setattr</a>, <a href="/recipes/tags/__setattr__/">__setattr__</a>). Revision 2. </p> <p>This is a Python implementation of the observer pattern described by Gamma et. al. It defines a one-to many dependency between objects so that when one object changes state, all its dependents (i.e. observers) are notified and updated automatically.</p> <p>My adaptation gets rid of the need to use specific functions to set the data (and to call Notify) and allows you to be notified for ANY attribute you set. It is possible to specify a list of attributes which should not trigger a notification. In case you need the opposite, it is very easy to invert the behavior of the code.</p> <p>The example should output: Creating data1 without notification for attrs name &amp; surname <br /> Creating data2 without notification for attr age <br /> Setting data1.name=Heather - Notification unnecessary <br /> Setting data1.num=333 - Notification expected <br /> Observer1: Subject Heather has updated attr num to 333 <br /> Setting data2.name=Molly - Notification expected <br /> Observer2: Subject Molly has updated attr name to Molly <br /> Setting data2.age=28 - Notification unnecessary <br /> Setting data2.eyecolor=blue - Notification expected <br /> Observer2: Subject Molly has updated attr eyecolor to blue </p> Observer pattern implemented with Descriptor class (Python) 2012-02-14T23:43:53-08:00Rodney Drenthhttp://code.activestate.com/recipes/users/4050661/http://code.activestate.com/recipes/578038-observer-pattern-implemented-with-descriptor-class/ <p style="color: grey"> Python recipe 578038 by <a href="/recipes/users/4050661/">Rodney Drenth</a> (<a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/reactive/">reactive</a>, <a href="/recipes/tags/subscribe/">subscribe</a>). </p> <p>The observer pattern is implemented using an observable descriptor. One creates instances of observable in a class, which allows observers to subscribe to changes in the observable. Assigning a value to the observable causes the suscribers to be notified. An observable can subscribe to another observable, in which case changes to the second propagate to subscribers of the first. The subscribe method returns a Subscription object. When this object is deleted or becomes unreferenced, the subscription is cancelled.</p> <p>This version compatible with Python 3.0 Example uses unittest to help in understanding the functionality.</p> Observer Design Pattern for python gevent coroutine package (Python) 2010-12-08T08:33:30-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577491-observer-design-pattern-for-python-gevent-coroutin/ <p style="color: grey"> Python recipe 577491 by <a href="/recipes/users/4176176/">Andrey Nikishaev</a> (<a href="/recipes/tags/event/">event</a>, <a href="/recipes/tags/gevent/">gevent</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is simple implementation of the observer design pattern. Acting as a registration hub, it fires events when requested. Also i have gevent.Timeout like interface in situations when you need to run event-method in the same greenlet. Example: </p> <pre class="prettyprint"><code>e = Observer() ev = e.wait('kill') try: gevent.sleep(3) except FiredEvent: print 'Fired!' else: print 'Not Fired!' finally: ev.cancel() </code></pre> <p>But rememeber, if you are using subscribe method, event-method will be executed in another greenlet.</p> Threadsafe observer pattern implemented as descriptor (Python) 2010-03-13T12:17:11-08:00Rodney Drenthhttp://code.activestate.com/recipes/users/4050661/http://code.activestate.com/recipes/577106-threadsafe-observer-pattern-implemented-as-descrip/ <p style="color: grey"> Python recipe 577106 by <a href="/recipes/users/4050661/">Rodney Drenth</a> (<a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/subscribe/">subscribe</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>This is a threadsafe version of <a href="http://code.activestate.com/recipes/576979/">recipe 576979</a>. A publish-subscribe (observer) pattern is implemented as a descriptor. Assigning a value notifies the observers. Uses <a href="http://code.activestate.com/recipes/577105/">recipe 577105</a> as synchlock.py and <a href="http://code.activestate.com/recipes/576979/">recipe 576979</a> as Observer.py</p> Observer pattern implemented with Descriptor class (Python) 2010-03-13T11:53:18-08:00Rodney Drenthhttp://code.activestate.com/recipes/users/4050661/http://code.activestate.com/recipes/576979-observer-pattern-implemented-with-descriptor-class/ <p style="color: grey"> Python recipe 576979 by <a href="/recipes/users/4050661/">Rodney Drenth</a> (<a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/subscribe/">subscribe</a>). Revision 5. </p> <p>The observer pattern is implemented using an observable descriptor. One creates instances of observable in a class, which allows observers to subscribe to changes in the observable. Assigning a value to the observable causes the suscribers to be notified. An observable can subscribe to another observable, in which case changes to the second propagate to subscribers of the first. The subscribe method returns a Subscription object. When this object is deleted or becomes unreferenced, the subscription is cancelled.</p> Flexible observer pattern implementation (Python) 2012-12-06T19:23:11-08:00Glenn Eychanerhttp://code.activestate.com/recipes/users/4172294/http://code.activestate.com/recipes/576962-flexible-observer-pattern-implementation/ <p style="color: grey"> Python recipe 576962 by <a href="/recipes/users/4172294/">Glenn Eychaner</a> (<a href="/recipes/tags/event/">event</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/subscribe/">subscribe</a>, <a href="/recipes/tags/threadsafe/">threadsafe</a>). Revision 16. </p> <p>A simple, flexible, general-purpose observer pattern.</p> <p>Observers can be callable objects or objects with a particular named method (handle_notify() by default). Events can be any object, and observers can select which events they are interested in receiving. Support for a number of different types of lightweight event objects is included.</p> Yet another signal/slot implementation in Python (Python) 2008-09-01T23:21:28-07:00Thiago Marcos P. Santoshttp://code.activestate.com/recipes/users/4166797/http://code.activestate.com/recipes/576477-yet-another-signalslot-implementation-in-python/ <p style="color: grey"> Python recipe 576477 by <a href="/recipes/users/4166797/">Thiago Marcos P. Santos</a> (<a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/slot/">slot</a>, <a href="/recipes/tags/subscribe/">subscribe</a>, <a href="/recipes/tags/weakref/">weakref</a>). </p> <p>This code snippet was based on the nice <a href="http://code.activestate.com/recipes/439356/">recipe 439356</a> made by Patrick Chasco. My implementation supports only class methods callbacks. I'm keeping the idea of use weakrefs to avoid the interpreter keep the object allocated because the signal is registered (i.e. the signal object holds a reference to callback method). IMO the usage of WeakValueDictionary made the code smaller and clear and also are maintenance-free (when the object is collect by the garbage collector the signal is automatically unregistered). </p>