Popular Python recipes tagged "meta:requires=copy"http://code.activestate.com/recipes/langs/python/tags/meta:requires=copy/2016-02-25T19:40:33-08:00ActiveState Code RecipesPython 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> Fluent API method decorator (Python) 2015-07-08T13:20:58-07:00Oscar Byrnehttp://code.activestate.com/recipes/users/4192487/http://code.activestate.com/recipes/579078-fluent-api-method-decorator/ <p style="color: grey"> Python recipe 579078 by <a href="/recipes/users/4192487/">Oscar Byrne</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/fluent/">fluent</a>, <a href="/recipes/tags/interface/">interface</a>). </p> <p>A decorator for producing robust fluent API interfaces by returning a copy of self</p> The Game of Battleships in Python (Python) 2014-02-22T06:13:41-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578836-the-game-of-battleships-in-python/ <p style="color: grey"> Python recipe 578836 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Another fun game that is fun to program and play. No special AI (yet). But still and entertaining game. </p> <p>For more info about <a href="http://thelivingpearl.com/2014/02/17/the-game-of-battleships-in-python/">Battleships in Python</a> follow the link. </p> Shortest Common Supersequence algorithms (Python) 2013-10-02T12:52:23-07:00Rutger Saalminkhttp://code.activestate.com/recipes/users/4187940/http://code.activestate.com/recipes/578678-shortest-common-supersequence-algorithms/ <p style="color: grey"> Python recipe 578678 by <a href="/recipes/users/4187940/">Rutger Saalmink</a> (<a href="/recipes/tags/approximation/">approximation</a>, <a href="/recipes/tags/bound/">bound</a>, <a href="/recipes/tags/breadth_first_search/">breadth_first_search</a>, <a href="/recipes/tags/common/">common</a>, <a href="/recipes/tags/depth_first_search/">depth_first_search</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/shortest/">shortest</a>, <a href="/recipes/tags/super/">super</a>). </p> <p>The Shortest Common Supersequence (SCS) problem is an NP-hard problem (<a href="https://en.wikipedia.org/wiki/Shortest_common_supersequence" rel="nofollow">https://en.wikipedia.org/wiki/Shortest_common_supersequence</a>), which occurs in problems originating from various domains, e.g. Bio Genetics. Given a set of strings, the common supersequence of minimal length is sought after. Below a set of algorithms is given, which I used in approximating and/or backtracking the optimal solution(s). </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> Reader-Writer lock with priority for writers (Python) 2011-09-28T21:45:04-07:00Mateusz Koboshttp://code.activestate.com/recipes/users/4178730/http://code.activestate.com/recipes/577803-reader-writer-lock-with-priority-for-writers/ <p style="color: grey"> Python recipe 577803 by <a href="/recipes/users/4178730/">Mateusz Kobos</a> (<a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/threads/">threads</a>). Revision 2. </p> <p>The following class implements a reader-writer lock to use in the second readers-writers problem with python threads. In this problem, many readers can simultaneously access a share, and a writer has an exclusive access to this share. Additionally, the following constraints should be met: 1) no reader should be kept waiting if the share is currently opened for reading unless a writer is also waiting for the share, 2) no writer should be kept waiting for the share longer than absolutely necessary.</p> Taxi Fare Splitter (Python) 2012-04-27T02:10:56-07:00James Coliinshttp://code.activestate.com/recipes/users/4167379/http://code.activestate.com/recipes/578115-taxi-fare-splitter/ <p style="color: grey"> Python recipe 578115 by <a href="/recipes/users/4167379/">James Coliins</a> (<a href="/recipes/tags/barganing/">barganing</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/optimisation/">optimisation</a>). </p> <p>A method for allocating costs 'fairly' amongst a group of friends who cooperate to their mutual advantage.</p> Bunch class created from attributes in class (Python) 2011-12-31T18:03:02-08:00Fabio Zadroznyhttp://code.activestate.com/recipes/users/4180406/http://code.activestate.com/recipes/577999-bunch-class-created-from-attributes-in-class/ <p style="color: grey"> Python recipe 577999 by <a href="/recipes/users/4180406/">Fabio Zadrozny</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/bunch/">bunch</a>, <a href="/recipes/tags/metaclass/">metaclass</a>). Revision 3. </p> <p>Provide a way to construct class __init__, __slots__, __eq__, __ne__, __repr__ for the class and makes explicit which attributes each instance will have (and providing defaults).</p> <p>The __main__ session shows an example of how it should be used.</p> Rudimentary Database Engine (Python) 2013-09-02T01:51:53-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577825-rudimentary-database-engine/ <p style="color: grey"> Python recipe 577825 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/engine/">engine</a>, <a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/experiment/">experiment</a>). Revision 2. </p> <p>This module was written for self-academic purposes in an attempt to understand databases better. Just as in college where students are required to reinvent "wheels" like linked lists, trees, binary searches, et cetera, developing this program helped me understand some of the things that goes on behind the scenes in a database while also helping to learn what the desired output of each operation should be. The code in this module provides support for running a simple database engine that runs completely in memory and allows usage of various concepts available in a structured query language to get and set data that may be saved to file.</p> Expectation-Maximization (Python) 2011-06-04T11:02:18-07:00Gabriel Synnaevehttp://code.activestate.com/recipes/users/4178198/http://code.activestate.com/recipes/577735-expectation-maximization/ <p style="color: grey"> Python recipe 577735 by <a href="/recipes/users/4178198/">Gabriel Synnaeve</a> (<a href="/recipes/tags/data_mining/">data_mining</a>, <a href="/recipes/tags/machine_learning/">machine_learning</a>). Revision 3. </p> <p>Quick and simple implementation of Gaussian mixture model (with same covariance shapes) based expectation-maximization algorithm.</p> K-means (Python) 2011-06-04T10:58:09-07:00Gabriel Synnaevehttp://code.activestate.com/recipes/users/4178198/http://code.activestate.com/recipes/577734-k-means/ <p style="color: grey"> Python recipe 577734 by <a href="/recipes/users/4178198/">Gabriel Synnaeve</a> (<a href="/recipes/tags/data_mining/">data_mining</a>, <a href="/recipes/tags/machine_learning/">machine_learning</a>). Revision 2. </p> <p>Hard and soft k-means implemented simply in python (with numpy). Quick and dirty, tested and works on large (10k+ observations, 2-10 features) real-world data.</p> DictSet - A specialized Python container datatype for managing collections of sets. (Python) 2011-05-18T21:18:54-07:00Roger Lewhttp://code.activestate.com/recipes/users/4178017/http://code.activestate.com/recipes/577702-dictset-a-specialized-python-container-datatype-fo/ <p style="color: grey"> Python recipe 577702 by <a href="/recipes/users/4178017/">Roger Lew</a> (<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/development/">development</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/information/">information</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/sets/">sets</a>). </p> <p>The basic Python container types (dict, list, set, and tuple) are extremely versatile and powerful. The collections module first implemented in Python 2.4 has shown that sub-classing these containers can yield elegant solutions to the right problem. In a similar vein this project is a dict subclass for elegantly handling collections of sets. In many aspects a DictSet is similiar to a defaultdict of sets except it generalizes many of the set operations to the dict.</p> <p>Put simply, DictSet is a dict of sets that behaves like a set.</p> <p>DictSet requires 0 non-standard dependencies and should work with Python 2.5 and up.</p> Named Sequences for environments containing large numbers of POD instances (Python) 2010-11-27T13:55:18-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577473-named-sequences-for-environments-containing-large-/ <p style="color: grey"> Python recipe 577473 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/slot/">slot</a>, <a href="/recipes/tags/unittests/">unittests</a>). </p> <p>Generate classes with named data attributes that can be sequenced. Useful for POD classes for which many records will exist concurrently.</p> <p>Compare the feature set to NamedTuples by Raymond Hettinger: <a href="http://code.activestate.com/recipes/500261-named-tuples/" rel="nofollow">http://code.activestate.com/recipes/500261-named-tuples/</a></p> Penrose Tiler (Python) 2010-09-22T05:22:28-07:00James Coliinshttp://code.activestate.com/recipes/users/4167379/http://code.activestate.com/recipes/577405-penrose-tiler/ <p style="color: grey"> Python recipe 577405 by <a href="/recipes/users/4167379/">James Coliins</a> (<a href="/recipes/tags/math/">math</a>). </p> <p>Covers a plane with two types of tiles, (Perose tiles). The pattern is interesting as it is nonperiodic and has a five fold symetry</p> Transactionable Objects (reloaded) (Python) 2008-03-23T15:09:47-07:00Harald Hoyerhttp://code.activestate.com/recipes/users/4135794/http://code.activestate.com/recipes/551788-transactionable-objects-reloaded/ <p style="color: grey"> Python recipe 551788 by <a href="/recipes/users/4135794/">Harald Hoyer</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>This class allows sub-classes to commit changes to an instance to a history, and rollback to previous states.</p> Simplify printing using the string template feature added in 2.4 (Python) 2008-02-23T08:36:18-08:00Tim Keatinghttp://code.activestate.com/recipes/users/2246480/http://code.activestate.com/recipes/543272-simplify-printing-using-the-string-template-featur/ <p style="color: grey"> Python recipe 543272 by <a href="/recipes/users/2246480/">Tim Keating</a> (<a href="/recipes/tags/text/">text</a>). Revision 3. </p> <p>The alternate substitution delimiter introduced in 2.4 (string.Template) was supposed to make string substitution easier. In fact it is a little cumbersome to use. This recipe employs a little stack hackery to make it as easy as it ought to be.</p> emulate collections.defaultdict (Python) 2007-07-09T14:15:39-07:00Jason Kirtlandhttp://code.activestate.com/recipes/users/4067388/http://code.activestate.com/recipes/523034-emulate-collectionsdefaultdict/ <p style="color: grey"> Python recipe 523034 by <a href="/recipes/users/4067388/">Jason Kirtland</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>A pure-Python version of Python 2.5's defaultdict</p> Copying Generators (Python) 2007-10-09T07:38:06-07:00kay schluehrhttp://code.activestate.com/recipes/users/2398921/http://code.activestate.com/recipes/528949-copying-generators/ <p style="color: grey"> Python recipe 528949 by <a href="/recipes/users/2398921/">kay schluehr</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>This recipe presents copy_generator(...) which a pure Python function keeping a running generator object and returns a copy of the generator object being in the same state as the original generator object.</p> A generator for an arbitrary number of 'for' loops (Python) 2007-01-30T09:16:34-08:00Colin Gillespiehttp://code.activestate.com/recipes/users/4029798/http://code.activestate.com/recipes/502194-a-generator-for-an-arbitrary-number-of-for-loops/ <p style="color: grey"> Python recipe 502194 by <a href="/recipes/users/4029798/">Colin Gillespie</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Python has a number of nice methods to handle 'for' loops. However, the situation often arises where you have a large number of nested loops. Using this solution reduces the number of loops to one.</p> Implementing an Immutable Dictionary (Python) 2007-06-13T09:23:09-07:00Aristotelis Mikropouloshttp://code.activestate.com/recipes/users/2881737/http://code.activestate.com/recipes/498072-implementing-an-immutable-dictionary/ <p style="color: grey"> Python recipe 498072 by <a href="/recipes/users/2881737/">Aristotelis Mikropoulos</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 7. </p> <p>The implementation of a dictionary, whose items cannot be reset or deleted, nor new can be added.</p>