Popular recipes by Raymond Hettinger http://code.activestate.com/recipes/users/178123/2016-08-07T22:02:10-07:00ActiveState Code RecipesFast 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> 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> Auto differentiation (Python) 2016-08-07T22:02:10-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/580610-auto-differentiation/ <p style="color: grey"> Python recipe 580610 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/autodifferentiation/">autodifferentiation</a>, <a href="/recipes/tags/calculus/">calculus</a>, <a href="/recipes/tags/descent/">descent</a>, <a href="/recipes/tags/gradient/">gradient</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/vector/">vector</a>). Revision 5. </p> <p>Directly computes derivatives from ordinary Python functions using auto differentiation. The technique directly computes the desired derivatives to full precision without resorting to symbolic math and without making estimates bases on numerical methods.</p> <p>The module provides a Num class for "dual" numbers that performs both regular floating point math on a value and its derivative at the same time. In addition, the module provides drop-in substitutes for most of the functions in the math module. There are also tools for partial derivatives, directional derivatives, gradients of scalar fields, and the curl and divergence of vector fields.</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> 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> 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> 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> 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> 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 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> Simple tool for simulating classes using closures and nested scopes (Python) 2012-04-11T06:21:18-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578091-simple-tool-for-simulating-classes-using-closures-/ <p style="color: grey"> Python recipe 578091 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/closure/">closure</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/oriented/">oriented</a>). Revision 9. </p> <p>Closured-based alternative to normal classes. Allows a faster, cleaner coding style at the expense of some functionality.</p> Colorize Python -- Sourcecode Syntax Highlighting (Python) 2012-07-21T02:46:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578178-colorize-python-sourcecode-syntax-highlighting/ <p style="color: grey"> Python recipe 578178 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/highlighting/">highlighting</a>, <a href="/recipes/tags/syntax/">syntax</a>). Revision 20. </p> <p>Generates colorized HTML, ANSI escaped text, or a LaTeX document from Python source code. Useful for publishing or viewing your code in a more readable way.</p> Pi Circle (Python) 2012-05-13T04:35:46-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578130-pi-circle/ <p style="color: grey"> Python recipe 578130 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/art/">art</a>, <a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/fun/">fun</a>, <a href="/recipes/tags/pi/">pi</a>). Revision 5. </p> <p>Computes Pi to many decimal places and prints the digits in a circle.</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> Nested contexts -- a chain of mapping objects (Python) 2010-10-25T02:13:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577434-nested-contexts-a-chain-of-mapping-objects/ <p style="color: grey"> Python recipe 577434 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/chained/">chained</a>, <a href="/recipes/tags/compiler/">compiler</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/nonlocal/">nonlocal</a>, <a href="/recipes/tags/scopes/">scopes</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>Easy to use chain of dictionaries for crafting nested scopes or for a tree of scopes. Useful for analyzing AST nodes, XML nodes or other structures with multiple scopes. Can emulate various chaining styles including static/lexical scoping, dynamic scoping and Python's own globals(), locals(), nested scopes, and writeable nonlocals. Can also model Python's inheritance chains: instance dictionary, class dictionary, and base classes.</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> Simplified, highly optimized LRU Cache (Python) 2011-12-01T00:57:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577969-simplified-highly-optimized-lru-cache/ <p style="color: grey"> Python recipe 577969 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/lru/">lru</a>). Revision 3. </p> <p>An O(1) LRU cache. Short, sweet, and fast. No bells and whistles.</p> Simplified LRU Cache (Python) 2013-03-06T06:04:18-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577970-simplified-lru-cache/ <p style="color: grey"> Python recipe 577970 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>An O(1) LRU cache. Short, sweet, and fast.</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> Public Key Encryption (RSA) (Python) 2012-05-12T23:34:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577737-public-key-encryption-rsa/ <p style="color: grey"> Python recipe 577737 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/inverse/">inverse</a>, <a href="/recipes/tags/multiplicative/">multiplicative</a>, <a href="/recipes/tags/primality_testing/">primality_testing</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/publickey/">publickey</a>, <a href="/recipes/tags/rsa/">rsa</a>). Revision 2. </p> <p>Simple code to create and use public/private keypairs. Accompanied by a rudimentary encoder.</p>