Top-rated recipes by Raymond Hettinger http://code.activestate.com/recipes/users/178123/top/2013-01-17T09:28:24-08:00ActiveState Code RecipesDecorator for BindingConstants at compile time (Python) 2010-11-16T08:36:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time/ <p style="color: grey"> Python recipe 277940 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 9. </p> <p>Decorator for automatic code optimization. If a global is known at compile time, replace it with a constant. Fold tuples of constants into a single constant. Fold constant attribute lookups into a single constant.</p> Spreadsheet (Python) 2004-12-03T09:13:43-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/355045-spreadsheet/ <p style="color: grey"> Python recipe 355045 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 2. </p> <p>Use eval() to drive spreadsheet style logic. The sleeper feature of Py2.4 is the ability to use any object with a mapping interface as the locals argument to eval().</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> Groupby (Python) 2004-02-12T07:05:02-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/259173-groupby/ <p style="color: grey"> Python recipe 259173 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>Guido inspired SQL-like GROUPBY class that also encapsulates the logic in a Unix-like "sort | uniq".</p> Bloom Filter (Python) 2011-06-04T17:44:21-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577684-bloom-filter/ <p style="color: grey"> Python recipe 577684 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/big_table/">big_table</a>, <a href="/recipes/tags/bloom_filter/">bloom_filter</a>, <a href="/recipes/tags/sets/">sets</a>, <a href="/recipes/tags/spelling_checker/">spelling_checker</a>, <a href="/recipes/tags/spell_checker/">spell_checker</a>). Revision 18. </p> <p>Space efficient, probabilistic set membership tester. Has no False Negatives but allows a rare False Positive.</p> Proof-of-concept for a more space-efficient, faster-looping dictionary (Python) 2013-01-17T09:28:24-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578375-proof-of-concept-for-a-more-space-efficient-faster/ <p style="color: grey"> Python recipe 578375 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/compact/">compact</a>, <a href="/recipes/tags/dictionary/">dictionary</a>). Revision 20. </p> <p>Save space and improve iteration speed by moving the hash/key/value entries to a densely packed array keeping only a sparse array of indices. This eliminates wasted space without requiring any algorithmic changes.</p> Dirt simple map/reduce (Python) 2011-05-15T16:46:55-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577676-dirt-simple-mapreduce/ <p style="color: grey"> Python recipe 577676 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/crosstab/">crosstab</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/map_reduce/">map_reduce</a>, <a href="/recipes/tags/pivot_table/">pivot_table</a>, <a href="/recipes/tags/statistics/">statistics</a>). Revision 9. </p> <p>Simple tool for analyzing datasets.</p> Compare speeds of different kinds of access to variables (Python) 2011-08-10T23:54:12-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577834-compare-speeds-of-different-kinds-of-access-to-var/ <p style="color: grey"> Python recipe 577834 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/optimization/">optimization</a>). Revision 5. </p> <p>Compare speeds of locals, nested scopes, global, builtins, instance variables, and class variables.</p> OrderedSet (Python) 2012-12-19T07:12:32-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576694-orderedset/ <p style="color: grey"> Python recipe 576694 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/ordered/">ordered</a>, <a href="/recipes/tags/set/">set</a>). Revision 9. </p> <p>Set that remembers original insertion order.</p> LRU and LFU cache decorators (Python) 2010-08-01T01:19:23-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/ <p style="color: grey"> Python recipe 498245 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 6. </p> <p>One-line decorator call adds caching to functions with hashable arguments and no keyword arguments. When the maximum size is reached, the least recently used entry or least frequently used entry is discarded -- appropriate for long-running processes which cannot allow caches to grow without bound. Includes built-in performance instrumentation.</p> How to use super() effectively -- Python 2.7 version (Python) 2011-06-01T19:19:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577721-how-to-use-super-effectively-python-27-version/ <p style="color: grey"> Python recipe 577721 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>). Revision 8. </p> <p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p> SortedCollection (Python) 2010-09-01T02:12:33-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577197-sortedcollection/ <p style="color: grey"> Python recipe 577197 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/bisect/">bisect</a>, <a href="/recipes/tags/collection/">collection</a>, <a href="/recipes/tags/sorted/">sorted</a>). Revision 9. </p> <p>Wraps bisect.bisect() in an easy to use class that supports key-functions and straight-forward search methods.</p> Eight queens. Six lines. (Python) 2009-02-10T14:59:07-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576647-eight-queens-six-lines/ <p style="color: grey"> Python recipe 576647 by <a href="/recipes/users/178123/">Raymond Hettinger</a> . Revision 3. </p> <p>What six lines of Python can do</p> Iterator Merge (Python) 2007-02-09T18:01:54-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/491285-iterator-merge/ <p style="color: grey"> Python recipe 491285 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 3. </p> <p>Memory efficient multi-way iterator merge.</p> How to use super() effectively (Python) 2011-06-01T19:17:58-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577720-how-to-use-super-effectively/ <p style="color: grey"> Python recipe 577720 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>). Revision 8. </p> <p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p> Compute Memory footprint of an object and its contents (Python) 2012-11-23T23:57:31-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577504-compute-memory-footprint-of-an-object-and-its-cont/ <p style="color: grey"> Python recipe 577504 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>). Revision 3. </p> <p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p> Technique for cyclical iteration (Python) 2009-12-03T23:08:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/ <p style="color: grey"> Python recipe 576961 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/concurrency_model/">concurrency_model</a>, <a href="/recipes/tags/cyclic_iterator/">cyclic_iterator</a>). Revision 7. </p> <p>Solution to the Hamming Number problem. Demonstrates a lazy evaluation evaluation technique using itertools.tee() to feed an iterator into itself. This is a common technique with Haskell. The deferred_output() function is the key technique for implementing a forward reference to the output of the stream.</p> Total ordering class decorator (Python) 2010-09-07T05:47:25-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576685-total-ordering-class-decorator/ <p style="color: grey"> Python recipe 576685 by <a href="/recipes/users/178123/">Raymond Hettinger</a> . Revision 11. </p> <p>Given a class defining one or more ordering methods, this decorator supplies the rest. This simplifies and speeds-up the approach taken in <a href="http://code.activestate.com/recipes/576529/">recipe 576529</a>.</p> Persistent dict with multiple standard file formats (Python) 2011-09-06T20:01:46-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/ <p style="color: grey"> Python recipe 576642 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/dbm/">dbm</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/persistent/">persistent</a>, <a href="/recipes/tags/shelve/">shelve</a>). Revision 10. </p> <p>dbdict: a dbm based on a dict subclass.</p> <p>On open, loads full file into memory. On close, writes full dict to disk (atomically). Supported output file formats: csv, json, and pickle. Input file format automatically discovered.</p> <p>Usable by the shelve module for fast access.</p> Lightweight Unittester (Python) 2009-04-07T19:47:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/572194-lightweight-unittester/ <p style="color: grey"> Python recipe 572194 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 4. </p> <p>Inspired by py.test, the unittester is a single, simple function that is easily customized. The main virtue is that tests are much clearer than with the unittest.py.</p>