Popular recipes tagged "observer" but not "pattern"http://code.activestate.com/recipes/tags/observer-pattern/2012-02-14T23:43:53-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>
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>
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>