Popular Python recipes tagged "meta:requires=doctest"http://code.activestate.com/recipes/langs/python/tags/meta:requires=doctest/2014-08-02T02:56:12-07:00ActiveState Code RecipesYet Another NamedTuple (Python) 2014-08-02T02:56:12-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578918-yet-another-namedtuple/ <p style="color: grey"> Python recipe 578918 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Yet another look at Raymond Hettinger's excellent "namedtuple" factory. Unlike Raymond's version, this one minimizes the use of exec. In the original, the entire inner class is dynamically generated then exec'ed, leading to the bulk of the code being inside a giant string template. This version uses a regular inner class for everything except the __new__ method.</p> Design by contract on python vanilla (Python) 2013-11-05T20:50:30-08:00Alan Cristhian Ruizhttp://code.activestate.com/recipes/users/4186199/http://code.activestate.com/recipes/578767-design-by-contract-on-python-vanilla/ <p style="color: grey"> Python recipe 578767 by <a href="/recipes/users/4186199/">Alan Cristhian Ruiz</a> (<a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/design_pattern/">design_pattern</a>, <a href="/recipes/tags/productivity/">productivity</a>). Revision 2. </p> <p>The code below is an example that shows that we can do design by contract in python without any non-standard module.</p> bubblebabble (Python) 2012-11-03T09:28:41-07:00Space Hobohttp://code.activestate.com/recipes/users/4184146/http://code.activestate.com/recipes/578319-bubblebabble/ <p style="color: grey"> Python recipe 578319 by <a href="/recipes/users/4184146/">Space Hobo</a> (<a href="/recipes/tags/babble/">babble</a>, <a href="/recipes/tags/bubble/">bubble</a>, <a href="/recipes/tags/digest/">digest</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/readable/">readable</a>). </p> <p>This module provides a bubblebabble function, which computes a (somewhat more) human readable format for message digests.</p> Using doctests to verify a module's export list (Python) 2012-09-19T19:13:20-07:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/578266-using-doctests-to-verify-a-modules-export-list/ <p style="color: grey"> Python recipe 578266 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/doctest/">doctest</a>, <a href="/recipes/tags/export/">export</a>, <a href="/recipes/tags/module/">module</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>If you aren't very careful, modules can end up exporting more symbols than you intend. For example, everything that you import is added to your module's name-space. Then there's scaffolding and other stuff intended for internal use. The usual advice is to name everything with a leading underscore, but that gets complicated fast: "import sys as _sys", "from os import environ as _environ". The alternative is to use "__all__ " to define exactly what you want to export, but then you need to maintain it as you add things to your module. <em>Or do you?</em></p> Credit card number check (Python) 2013-08-12T14:11:25-07:00Matt Joneshttp://code.activestate.com/recipes/users/4182764/http://code.activestate.com/recipes/578202-credit-card-number-check/ <p style="color: grey"> Python recipe 578202 by <a href="/recipes/users/4182764/">Matt Jones</a> (<a href="/recipes/tags/validation/">validation</a>). Revision 2. </p> <p>Pass in the credit card number and the card type and get back a result with an explanation.</p> Inner Join (Python) 2011-11-01T20:10:34-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577937-inner-join/ <p style="color: grey"> Python recipe 577937 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/join/">join</a>, <a href="/recipes/tags/sql/">sql</a>). Revision 2. </p> <p>Implemented an SQL style INNER JOIN for two lists of tuples to be joined on a common field.</p> Unit Conversions Using Decimal (Python) 2011-04-13T06:55:05-07:00Dave Baileyhttp://code.activestate.com/recipes/users/4168479/http://code.activestate.com/recipes/577652-unit-conversions-using-decimal/ <p style="color: grey"> Python recipe 577652 by <a href="/recipes/users/4168479/">Dave Bailey</a> (<a href="/recipes/tags/conversions/">conversions</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/units/">units</a>). </p> <p>A table driven routine generates a unit conversion module and a doctest file. The generated module uses the Decimal class to create unit conversion classes which uses class properties to perform the conversion. conversion.py supplies the following classes: Distance Area Volumn Time Velocity Acceleration Mass Force Weight Pressure Frequency Power Temperature</p> <p>New classes and additional units can easily be created by adding to the table.</p> Counter class (Python) 2011-04-17T21:27:05-07:00Praveenhttp://code.activestate.com/recipes/users/4177693/http://code.activestate.com/recipes/577664-counter-class/ <p style="color: grey"> Python recipe 577664 by <a href="/recipes/users/4177693/">Praveen</a> . </p> <p>Bag/multiset class for convenient tallying of hashable objects.</p> An interval mapping data structure (Python) 2010-12-23T12:51:27-08:00Matteo Dell'Amicohttp://code.activestate.com/recipes/users/2433284/http://code.activestate.com/recipes/577515-an-interval-mapping-data-structure/ <p style="color: grey"> Python recipe 577515 by <a href="/recipes/users/2433284/">Matteo Dell'Amico</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/interval/">interval</a>, <a href="/recipes/tags/mapping/">mapping</a>). </p> <p>This structure is a kind of dictionary which allows you to map data intervals to values. You can then query the structure for a given point, and it returns the value associated to the interval which contains the point. Boundary values don't need to be an integer.</p> <p>In this version, the excellent <a href="http://pypi.python.org/pypi/blist/">blist</a> library by Daniel Stutzbach is used for efficiency. By using the collections.MutableMapping abstract base class, the whole signature of mappings is supported.</p> Auto Named Decriptors (Python) 2010-10-19T22:55:16-07:00Aloys Baillethttp://code.activestate.com/recipes/users/4175355/http://code.activestate.com/recipes/577426-auto-named-decriptors/ <p style="color: grey"> Python recipe 577426 by <a href="/recipes/users/4175355/">Aloys Baillet</a> (<a href="/recipes/tags/automatically/">automatically</a>, <a href="/recipes/tags/constants/">constants</a>, <a href="/recipes/tags/descriptors/">descriptors</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/name/">name</a>, <a href="/recipes/tags/string/">string</a>). Revision 3. </p> <p>Using named Descriptors? Tired of duplicating the name of the instance in a string? A small metaclass can solve this.</p> Sets with a custom equality/uniqueness function (Python) 2009-10-29T21:08:22-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576932-sets-with-a-custom-equalityuniqueness-function/ <p style="color: grey"> Python recipe 576932 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/abstract_base_class/">abstract_base_class</a>, <a href="/recipes/tags/mutableset/">mutableset</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/unique/">unique</a>). Revision 4. </p> <p>The builtin <code>set</code> and <code>frozenset</code> types are based on object equality; they call __eq__ to determine whether an object is a member of the set or not. But there are cases when one needs a set of objects that are compared by other means, apart from the default __eq__ function. There are several ways to achieve that; this recipe presents two classes, FrozenKeyedSet and KeyedSet, that take an additional function <code>key</code> which is used to determine membership and uniqueness. Given two objects which return the same value for <code>key</code>, only one of them will be in the set.</p> Counter class (Python) 2009-06-29T12:24:14-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576611-counter-class/ <p style="color: grey"> Python recipe 576611 by <a href="/recipes/users/178123/">Raymond Hettinger</a> . Revision 11. </p> <p>Bag/multiset class for convenient tallying of hashable objects.</p> Find all subclasses of a given class (Python) 2009-11-04T20:26:08-08:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576949-find-all-subclasses-of-a-given-class/ <p style="color: grey"> Python recipe 576949 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/extending/">extending</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/plugin/">plugin</a>, <a href="/recipes/tags/subclass/">subclass</a>, <a href="/recipes/tags/subclasses/">subclasses</a>, <a href="/recipes/tags/type/">type</a>). Revision 3. </p> <p>itersubclasses(cls) returns a generator over all subclasses of cls, in depth first order. cls must be a new-style class; old-style classes are <em>not</em> supported.</p> Named Tuples (Python) 2009-05-26T22:44:39-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/500261-named-tuples/ <p style="color: grey"> Python recipe 500261 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 15. </p> <p>Fast, lightweight attribute-style access to tuples.</p> AttrDict (Python) 2009-11-28T05:39:08-08:00Chris Joneshttp://code.activestate.com/recipes/users/4171447/http://code.activestate.com/recipes/576972-attrdict/ <p style="color: grey"> Python recipe 576972 by <a href="/recipes/users/4171447/">Chris Jones</a> (<a href="/recipes/tags/attr/">attr</a>, <a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/dict/">dict</a>). </p> <p>Dictionary object that can also be accessed via attributes</p> Flipdict -- python dict that also maintains a one-to-one inverse mapping (Python) 2009-12-03T14:43:52-08:00Francis Carrhttp://code.activestate.com/recipes/users/4172444/http://code.activestate.com/recipes/576968-flipdict-python-dict-that-also-maintains-a-one-to-/ <p style="color: grey"> Python recipe 576968 by <a href="/recipes/users/4172444/">Francis Carr</a> (<a href="/recipes/tags/1_to_1/">1_to_1</a>, <a href="/recipes/tags/bijection/">bijection</a>, <a href="/recipes/tags/bijective/">bijective</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/injection/">injection</a>, <a href="/recipes/tags/injective/">injective</a>, <a href="/recipes/tags/inverse/">inverse</a>, <a href="/recipes/tags/invert/">invert</a>, <a href="/recipes/tags/mapping/">mapping</a>, <a href="/recipes/tags/one_to_one/">one_to_one</a>). Revision 6. </p> <p>A Flipdict is a python dict subclass that maintains a one-to-one inverse mapping. Each key maps to a unique value, and each value maps back to that same key. Each instance has a "flip" attribute to access the inverse mapping.</p> format a number as an ordinal (Python) 2010-02-04T12:54:27-08:00Serdar Tumgorenhttp://code.activestate.com/recipes/users/4171495/http://code.activestate.com/recipes/576888-format-a-number-as-an-ordinal/ <p style="color: grey"> Python recipe 576888 by <a href="/recipes/users/4171495/">Serdar Tumgoren</a> (<a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/numerals/">numerals</a>, <a href="/recipes/tags/ordinals/">ordinals</a>). Revision 10. </p> <p>This function converts 0 and <em>positive</em> integers (or their string representations) to their ordinal values. For example, it would convert 0 to "0th", 1 to "1st", 2 to "2nd", 3 to "3rd" and so on.</p> Browser history data structure (Python) 2009-12-25T17:26:57-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576991-browser-history-data-structure/ <p style="color: grey"> Python recipe 576991 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/datastuctures/">datastuctures</a>). Revision 6. </p> <p>The <code>BrowserHistory</code> class encapsulates the history of moving from location to location, as in Web browsing context; the recipe is not restricted to Web browsing though. See docstrings for more details and usage.</p> <p>The current implementation requires Python 2.6.</p> Path entire split & commonprefix (Python) 2011-04-06T20:29:46-07:00Maxime Fontenierhttp://code.activestate.com/recipes/users/4172150/http://code.activestate.com/recipes/577016-path-entire-split-commonprefix/ <p style="color: grey"> Python recipe 577016 by <a href="/recipes/users/4172150/">Maxime Fontenier</a> (<a href="/recipes/tags/commonprefix/">commonprefix</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/split/">split</a>). Revision 5. </p> <p>This recipe is a kind of extension to os.path It offers an entire split function and a true commonprefix for paths.</p> Temporary substitution of object in modules via with statement (Python) 2010-01-24T04:19:12-08:00Jacob Oscarsonhttp://code.activestate.com/recipes/users/1355144/http://code.activestate.com/recipes/577020-temporary-substitution-of-object-in-modules-via-wi/ <p style="color: grey"> Python recipe 577020 by <a href="/recipes/users/1355144/">Jacob Oscarson</a> (<a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/with_statement/">with_statement</a>). </p> <p>See docstring in the code</p>