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