Latest recipes tagged "cascade"http://code.activestate.com/recipes/tags/cascade/new/2016-09-01T12:34:17-07:00ActiveState Code RecipesMethod chaining or cascading (Python) 2016-09-01T12:34:17-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578770-method-chaining-or-cascading/ <p style="color: grey"> Python recipe 578770 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/cascade/">cascade</a>, <a href="/recipes/tags/cascading/">cascading</a>, <a href="/recipes/tags/chaining/">chaining</a>, <a href="/recipes/tags/method/">method</a>). </p> <p>A frequently missed feature of built-ins like lists and dicts is the ability to chain method calls like this:</p> <pre class="prettyprint"><code>x = [] x.append(1).append(2).append(3).reverse().append(4) # x now equals [3, 2, 1, 4] </code></pre> <p>Unfortunately this doesn't work, as mutator methods return <code>None</code> rather than <code>self</code>. One possibility is to design your class from the beginning with method chaining in mind, but what do you do with those like the built-ins which aren't?</p> <p>This is sometimes called <a href="https://en.wikipedia.org/wiki/Method_cascading">method cascading</a>. Here's a proof-of-concept for an adapter class which turns any object into one with methods that can be chained.</p> Observer 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>