Popular recipes tagged "pattern"http://code.activestate.com/recipes/tags/pattern/2014-05-29T18:48:28-07:00ActiveState Code RecipesCreate on-the-fly class adapters with functools.partial (Python)
2014-05-29T18:48:28-07:00Christoph Schuelerhttp://code.activestate.com/recipes/users/4174094/http://code.activestate.com/recipes/578884-create-on-the-fly-class-adapters-with-functoolspar/
<p style="color: grey">
Python
recipe 578884
by <a href="/recipes/users/4174094/">Christoph Schueler</a>
(<a href="/recipes/tags/adapter/">adapter</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/partial/">partial</a>, <a href="/recipes/tags/pattern/">pattern</a>).
</p>
<p>functools.partial could not only applied to functions it also works with classes.
This opens some interesting perspectives, like on-the-fly creation of class
adapters, as the following code illustrates.</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 & 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>
Multiple unique class instances - a tentative design pattern (Python)
2013-02-23T14:11:44-08:00Moritz Beberhttp://code.activestate.com/recipes/users/4185350/http://code.activestate.com/recipes/578471-multiple-unique-class-instances-a-tentative-design/
<p style="color: grey">
Python
recipe 578471
by <a href="/recipes/users/4185350/">Moritz Beber</a>
(<a href="/recipes/tags/design_pattern/">design_pattern</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/unique/">unique</a>).
</p>
<p>Coming from Bioinformatics and having to deal with multiple objects with unique properties, like genes, proteins, and many more, I felt the need for a class that would always yield a unique instance based on a chosen identifier. This is because I always wanted the same instances whose attributes were filled with information and track them in various storage classes, like dictionaries, lists, etc. The code for the class lives <a href="https://github.com/Midnighter/unique-base">over on github</a>. Recently, I've added a lot of parallel-processing code so I adapted the pickling behaviour for this class to also yield existing instances. What follows is an example of how to use it and some discussion questions.</p>
Singleton? We don't need no stinkin' singleton: the Borg design pattern (Python)
2011-09-18T18:47:59-07:00Anler Hernández Peralhttp://code.activestate.com/recipes/users/4176323/http://code.activestate.com/recipes/577870-singleton-we-dont-need-no-stinkin-singleton-the-bo/
<p style="color: grey">
Python
recipe 577870
by <a href="/recipes/users/4176323/">Anler Hernández Peral</a>
(<a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/pattern/">pattern</a>).
</p>
<p>The Singleton design pattern (DP) has a catchy name, but the wrong focus -- on identity rather than on state. The Borg design pattern has all instances share state instead, and Python makes it, literally, a snap.</p>
Concrete Class Finder (Python)
2011-08-24T05:29:15-07:00Lucio Santihttp://code.activestate.com/recipes/users/4178886/http://code.activestate.com/recipes/577858-concrete-class-finder/
<p style="color: grey">
Python
recipe 577858
by <a href="/recipes/users/4178886/">Lucio Santi</a>
(<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/design/">design</a>, <a href="/recipes/tags/finder/">finder</a>, <a href="/recipes/tags/match/">match</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/suitable/">suitable</a>, <a href="/recipes/tags/switch/">switch</a>).
</p>
<p>This recipe implements a design pattern useful for performing an object-oriented case analysis for a particular object (or a collection of objects as well). Essentially, it is an alternative to complex if-then-else or switches. Modelling each case with a particular class, the Concrete Class Finder searches for an appropriate case/class that applies to the given object/s. Once found, this class can be used in an homogeneous way, independently of the object/s previously considered.</p>
Maybe pattern (Python)
2010-07-21T11:08:11-07:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/577248-maybe-pattern/
<p style="color: grey">
Python
recipe 577248
by <a href="/recipes/users/4169882/">Alan Franzoni</a>
(<a href="/recipes/tags/design/">design</a>, <a href="/recipes/tags/maybe/">maybe</a>, <a href="/recipes/tags/pattern/">pattern</a>).
Revision 2.
</p>
<p>An example Maybe pattern implementation for Python.</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>
Multicontext (e.g. asynchronous) inline execution framework using coroutines (Python)
2012-12-06T19:32:20-08:00Glenn Eychanerhttp://code.activestate.com/recipes/users/4172294/http://code.activestate.com/recipes/576965-multicontext-eg-asynchronous-inline-execution-fram/
<p style="color: grey">
Python
recipe 576965
by <a href="/recipes/users/4172294/">Glenn Eychaner</a>
(<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/inline/">inline</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/thread/">thread</a>).
Revision 14.
</p>
<p>A framework for executing inline code, contained in a generator, across multiple execution contexts, by pairing it with an executor that handles the context switching at each yield. An example of a generator which executes some iterations synchronously and some asynchronously is provided. The framework is general enough to be applied to many different coroutine situations.</p>
Maintenance free Factory design pattern (Python)
2009-03-12T08:54:39-07:00Akira Forahttp://code.activestate.com/recipes/users/4114779/http://code.activestate.com/recipes/576687-maintenance-free-factory-design-pattern/
<p style="color: grey">
Python
recipe 576687
by <a href="/recipes/users/4114779/">Akira Fora</a>
(<a href="/recipes/tags/design/">design</a>, <a href="/recipes/tags/factory/">factory</a>, <a href="/recipes/tags/free/">free</a>, <a href="/recipes/tags/maintenance/">maintenance</a>, <a href="/recipes/tags/pattern/">pattern</a>).
Revision 3.
</p>
<p>An implementation of the Factory design pattern that doesn't require maintenance when a new specialized subclass is added.</p>