Most viewed recipes by Raymond Hettinger http://code.activestate.com/recipes/users/178123/views/2013-03-06T05:38:15-08:00ActiveState Code RecipesOrderedSet (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>
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>
Ordered Dictionary for Py2.4 (Python)
2011-04-24T03:20:45-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/
<p style="color: grey">
Python
recipe 576693
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/linked_list/">linked_list</a>, <a href="/recipes/tags/ordered/">ordered</a>).
Revision 9.
</p>
<p>Drop-in substitute for Py2.7's new collections.OrderedDict. The recipe has big-oh performance that matches regular dictionaries (amortized O(1) insertion/deletion/lookup and O(n) iteration/repr/copy/equality_testing).</p>
How to Set Environment Variables (Python)
2002-10-27T15:35:32-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/159462-how-to-set-environment-variables/
<p style="color: grey">
Python
recipe 159462
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>Writes environment variables using a batch file wrapper. Overcomes an operating system limitation.</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>
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>
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>
Binary floating point summation accurate to full precision (Python)
2009-03-28T23:32:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/
<p style="color: grey">
Python
recipe 393090
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>Completely eliminates rounding errors and loss of significance due to catastrophic cancellation during summation. Achieves exactness by keeping full precision intermediate subtotals. Offers three alternative approaches, each using a different technique to store exact subtotals.</p>
DBF reader and writer (Python)
2009-04-29T07:28:14-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/362715-dbf-reader-and-writer/
<p style="color: grey">
Python
recipe 362715
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/database/">database</a>).
Revision 7.
</p>
<p>Reader iterates over records in Dbase or Xbase files.
Writer creates dbf files from Python sequences.</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>
Fast min/max function (Python)
2011-10-22T18:40:32-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577916-fast-minmax-function/
<p style="color: grey">
Python
recipe 577916
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/minmax/">minmax</a>, <a href="/recipes/tags/sorting/">sorting</a>).
</p>
<p>Minimizes the number of comparisons to compute the minimum and maximum of a dataset. Uses 1.5 compares for every element, improving on the more obvious solution that does 2 comparisons per element.</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>
Decorator 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>
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>
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>
Efficient Running Median using an Indexable Skiplist (Python)
2010-02-07T14:40:03-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576930-efficient-running-median-using-an-indexable-skipli/
<p style="color: grey">
Python
recipe 576930
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/advanced_data_structure/">advanced_data_structure</a>, <a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/new_algorithm/">new_algorithm</a>, <a href="/recipes/tags/skiplist/">skiplist</a>, <a href="/recipes/tags/sliding_window/">sliding_window</a>).
Revision 10.
</p>
<p>Maintains sorted data as new elements are added and old one removed as a sliding window advances over a stream of data. Also gives fast indexed access to value. Running time per median update is proportional to the log of the window size.</p>
Convert a cmp function to a key function (Python)
2010-04-04T23:28:40-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576653-convert-a-cmp-function-to-a-key-function/
<p style="color: grey">
Python
recipe 576653
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/2_to_3/">2_to_3</a>).
Revision 5.
</p>
<p>Py3.0 transition aid. The <em>sorted()</em> builtin and the <em>list.sort()</em> method no longer accept a <em>cmp</em> function in Python 3.0. Most cases are easy to convert manually. This recipe handles the remaining cases.</p>
bag collection class (Python)
2004-11-30T07:57:31-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/259174-bag-collection-class/
<p style="color: grey">
Python
recipe 259174
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 6.
</p>
<p>Implement Smalltalk's bag class (similar to MultiSets in C++ or bag's in Objective C or Haskell's Edison module).</p>
Py2.6+ and Py3.0+ backport of Python 3.3's LRU Cache (Python)
2013-03-06T05:38:15-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578078-py26-and-py30-backport-of-python-33s-lru-cache/
<p style="color: grey">
Python
recipe 578078
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/lru/">lru</a>).
Revision 6.
</p>
<p>Full-featured O(1) LRU cache backported from Python3.3. The full Py3.3 API is supported (thread safety, maxsize, keyword args, type checking, __wrapped__, and cache_info). Includes Py3.3 optimizations for better memory utilization, fewer dependencies, and fewer dict lookups.</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>