Popular recipes tagged "oop"http://code.activestate.com/recipes/tags/oop/popular/2016-07-31T04:03:29-07:00ActiveState Code RecipesEmulating super() in Python 3.x using Python 2.7 (Python) 2016-07-31T04:03:29-07:00sunqingyaohttp://code.activestate.com/recipes/users/4194518/http://code.activestate.com/recipes/580694-emulating-super-in-python-3x-using-python-27/ <p style="color: grey"> Python recipe 580694 by <a href="/recipes/users/4194518/">sunqingyao</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python2/">python2</a>). </p> <p>Depending on the name of the first argument, <code>self.__sup</code> or <code>cls.__sup</code> behaves like <code>super()</code> in Python 3, while this code is written in Python 2.7.</p> <p>It works for both ordinary methods and class methods(static methods don't use <code>super()</code>). See my code for detailed examples:</p> Python method chaining examples (Python) 2016-02-25T19:40:33-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580616-python-method-chaining-examples/ <p style="color: grey"> Python recipe 580616 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/chaining/">chaining</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This recipe shows a few examples of doing method chaining in Python.</p> Wrap a a file-like object in another that calls a user callback whenever read() is called on it. (Python) 2013-09-25T01:54:53-07:00Martin Millerhttp://code.activestate.com/recipes/users/155538/http://code.activestate.com/recipes/578674-wrap-a-a-file-like-object-in-another-that-calls-a-/ <p style="color: grey"> Python recipe 578674 by <a href="/recipes/users/155538/">Martin Miller</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/progress/">progress</a>, <a href="/recipes/tags/upload/">upload</a>). </p> <p>Wraps a file-like object in another, but also calls a user callback with the number of bytes read whenever its <code>read()</code> method is called. Used for tracking upload progress, for example for a progress bar in a UI application.</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> Observer Pattern (Python) 2013-02-04T20:55:10-08:00Ilya Pashchenkohttp://code.activestate.com/recipes/users/4185152/http://code.activestate.com/recipes/578449-observer-pattern/ <p style="color: grey"> Python recipe 578449 by <a href="/recipes/users/4185152/">Ilya Pashchenko</a> (<a href="/recipes/tags/oop/">oop</a>). </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 are notified and updated automatically.</p> <p>The example should output: Setting Data 1 = 10 DecimalViewer: Subject Data 1 has data 10 HexViewer: Subject Data 1 has data 0xa Setting Data 2 = 15 HexViewer: Subject Data 2 has data 0xf DecimalViewer: Subject Data 2 has data 15 Setting Data 1 = 3 DecimalViewer: Subject Data 1 has data 3 HexViewer: Subject Data 1 has data 0x3 Setting Data 2 = 5 HexViewer: Subject Data 2 has data 0x5 DecimalViewer: Subject Data 2 has data 5 Detach HexViewer from data1 and data2. Setting Data 1 = 10 DecimalViewer: Subject Data 1 has data 10 Setting Data 2 = 15 DecimalViewer: Subject Data 2 has data 15</p> Factory pattern (Python) 2013-02-04T21:12:27-08:00Ilya Pashchenkohttp://code.activestate.com/recipes/users/4185152/http://code.activestate.com/recipes/578450-factory-pattern/ <p style="color: grey"> Python recipe 578450 by <a href="/recipes/users/4185152/">Ilya Pashchenko</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>In the factory pattern you have an object that creates other objects.</p> Python: A event/signal dispatcher (Python) 2013-07-29T05:34:24-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578307-python-a-eventsignal-dispatcher/ <p style="color: grey"> Python recipe 578307 by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a> (<a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/signals/">signals</a>). </p> <p>This is a thread-safe dispatcher.</p> Bound Method Weakref (Python) 2013-07-29T05:35:55-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578298-bound-method-weakref/ <p style="color: grey"> Python recipe 578298 by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a> (<a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/weakref/">weakref</a>). Revision 4. </p> <p>This is a workaorund for weak reference bound methods. It may be useful for observer patterns.</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> Cache decorator in python 2.4 (Python) 2011-08-08T00:10:51-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577827-cache-decorator-in-python-24/ <p style="color: grey"> Python recipe 577827 by <a href="/recipes/users/4178890/">sivarama sarma</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>The latest version of Python introduced a new language feature, function and method decorators (PEP 318, <a href="http://www.python.org/peps/pep-0318.html" rel="nofollow">http://www.python.org/peps/pep-0318.html</a>). This recipe show a common callable transformation that can benefit from the new syntax, often referred to as Memoization pattern.</p> Infix operators (Python) 2011-06-06T12:07:00-07:00Animesh Kumarhttp://code.activestate.com/recipes/users/4178212/http://code.activestate.com/recipes/577738-infix-operators/ <p style="color: grey"> Python recipe 577738 by <a href="/recipes/users/4178212/">Animesh Kumar</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>Python has the wonderful "in" operator and it would be nice to have additional infix operator like this. This recipe shows how (almost) arbitrary infix operators can be defined.</p> Item Properties (Python) 2012-05-09T23:24:55-07:00Ian Kellyhttp://code.activestate.com/recipes/users/4178016/http://code.activestate.com/recipes/577703-item-properties/ <p style="color: grey"> Python recipe 577703 by <a href="/recipes/users/4178016/">Ian Kelly</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/property/">property</a>). Revision 2. </p> <p>A property variation that allows property access using an index or key.</p> Infix operators (Python) 2005-02-17T11:12:04-08:00Ferdinand Jamitzkyhttp://code.activestate.com/recipes/users/98863/http://code.activestate.com/recipes/384122-infix-operators/ <p style="color: grey"> Python recipe 384122 by <a href="/recipes/users/98863/">Ferdinand Jamitzky</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>Python has the wonderful "in" operator and it would be nice to have additional infix operator like this. This recipe shows how (almost) arbitrary infix operators can be defined.</p> Git Shell Script to enhance inline automation script... (Bash) 2010-11-01T00:43:03-07:00Patrick Riendeauhttp://code.activestate.com/recipes/users/4175653/http://code.activestate.com/recipes/577447-git-shell-script-to-enhance-inline-automation-scri/ <p style="color: grey"> Bash recipe 577447 by <a href="/recipes/users/4175653/">Patrick Riendeau</a> (<a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/git/">git</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/oriented/">oriented</a>, <a href="/recipes/tags/programs/">programs</a>, <a href="/recipes/tags/property/">property</a>, <a href="/recipes/tags/property_creation/">property_creation</a>, <a href="/recipes/tags/repository/">repository</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/shelve/">shelve</a>). </p> <p>This tiny effort, depend from Fnct.D ActiveState no.577446 Core, also available from github <a href="http://github.com/priendeau/Fnct.d" rel="nofollow">http://github.com/priendeau/Fnct.d</a>, can develop basic methodology of implementing oriented program within uses of function re-declaration with function-parser to create both property-function to discover uses of getter-function and setter-function</p> Fnct.D, comprehensive Bash-shell coding scheme. (Bash) 2010-11-01T00:29:09-07:00Patrick Riendeauhttp://code.activestate.com/recipes/users/4175653/http://code.activestate.com/recipes/577446-fnctd-comprehensive-bash-shell-coding-scheme/ <p style="color: grey"> Bash recipe 577446 by <a href="/recipes/users/4175653/">Patrick Riendeau</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/oriented/">oriented</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/system/">system</a>). </p> <p>A Bash compromising shell-module to pre-introduce object-oriented Bash-code.</p> Attribute-based Framework 1: Basics (Python) 2010-07-21T21:40:54-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577327-attribute-based-framework-1-basics/ <p style="color: grey"> Python recipe 577327 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/framework/">framework</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/simulation/">simulation</a>). Revision 2. </p> <p>I am adapting Prof. David Cheriton's OO software methodology to Python. It's an approach for building industrial-strength code with a disciplined architecture, consistent naming conventions, and a rigorous division of interface from implementation. I'll be adding more of his techniques in further recipes. These recipes are based on Cheriton's CS249a course at Stanford.</p> Attribute-based Framework 2: Notifications (Python) 2010-07-23T20:12:10-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577330-attribute-based-framework-2-notifications/ <p style="color: grey"> Python recipe 577330 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/framework/">framework</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/simulation/">simulation</a>). Revision 2. </p> <p>I am adapting Prof. David Cheriton's OO software methodology to Python. It's an approach for building industrial-strength code with a disciplined architecture, consistent naming conventions, and a rigorous division of interface from implementation. I'll be adding more of his techniques in further recipes. These recipes are based on Cheriton's CS249a course at Stanford.</p> PHP 'Struct' port (PHP) 2010-03-28T01:22:31-07:00Jeff Griffithshttp://code.activestate.com/recipes/users/835605/http://code.activestate.com/recipes/577160-php-struct-port/ <p style="color: grey"> PHP recipe 577160 by <a href="/recipes/users/835605/">Jeff Griffiths</a> (<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>In Ruby, the <a href="http://ruby-doc.org/core/classes/Struct.html">Struct class</a> is a convenient way to create a hash-like object on the fly and use it for your nefarious purposes. PHP 5+ can be convinced to do this type of things as well, it just doesn't have it out of the box. Here is a simple class that implements iterator and allows you to populate the internal data structure similar to how Ruby's Struct works. Syntactic sugar? Probably.</p> <p>Note: I haven't bothered to implement the Ruby Struct API per se, Instead I just got something similar by implementing the Iterator interface and keeping things very PHP-like.</p> Default arg special action (Python) 2010-04-14T19:52:33-07:00Ed Blakehttp://code.activestate.com/recipes/users/4173678/http://code.activestate.com/recipes/577194-default-arg-special-action/ <p style="color: grey"> Python recipe 577194 by <a href="/recipes/users/4173678/">Ed Blake</a> (<a href="/recipes/tags/default_arguments/">default_arguments</a>, <a href="/recipes/tags/identity/">identity</a>, <a href="/recipes/tags/oop/">oop</a>). </p> <p>A way to allow functions with 0, 1, and -1 arguments! For those times you want to differentiate a no value argument from a non-value argument.</p>