Popular recipes tagged "subscribe"http://code.activestate.com/recipes/tags/subscribe/popular/2012-12-06T19:23:11-08:00ActiveState Code RecipesObserver 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>
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>