Top-rated recipes tagged "meta:license=mit"http://code.activestate.com/recipes/tags/meta:license=mit/top/2014-09-06T10:35:54-07:00ActiveState Code RecipesBloom 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> 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> Python Binary Search Tree (Python) 2011-01-10T02:27:08-08:00Edward Loperhttp://code.activestate.com/recipes/users/2637812/http://code.activestate.com/recipes/577540-python-binary-search-tree/ <p style="color: grey"> Python recipe 577540 by <a href="/recipes/users/2637812/">Edward Loper</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/maximum/">maximum</a>, <a href="/recipes/tags/minimum/">minimum</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/sort/">sort</a>). Revision 2. </p> <p>A data structure that holds a sorted collection of values, and supports efficient insertion, deletion, sorted iteration, and min/max finding. Values may sorted either based on their natural ordering, or on a key function (specified as an argument to the search tree's constructor). The search tree may contain duplicate values (or multiple values with equal keys) -- the ordering of such values is undefined.</p> <p>This implementation was made with efficiency in mind. In particular, it is more than twice as fast as the other native-Python implementations I tried (which all use objects to store search tree nodes).</p> <p>See also: <a href="http://en.wikipedia.org/wiki/Binary_search_tree">http://en.wikipedia.org/wiki/Binary_search_tree</a>, <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">http://en.wikipedia.org/wiki/A*_search_algorithm</a></p> A-star Shortest Path Algorithm (C++) 2010-12-25T23:36:35-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577457-a-star-shortest-path-algorithm/ <p style="color: grey"> C++ recipe 577457 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/graphs/">graphs</a>, <a href="/recipes/tags/routes/">routes</a>). Revision 4. </p> <p>A-star (A*) is a shortest path algorithm widely used for RTS games, GPS navigation etc.</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> The goto decorator (Python) 2009-11-03T13:14:57-08:00Carl Cereckehttp://code.activestate.com/recipes/users/4172182/http://code.activestate.com/recipes/576944-the-goto-decorator/ <p style="color: grey"> Python recipe 576944 by <a href="/recipes/users/4172182/">Carl Cerecke</a> (<a href="/recipes/tags/bytecodes/">bytecodes</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/goto/">goto</a>). Revision 5. </p> <p>A function-decorator that provides the goto command.</p> Rsync Algorithm (Python) 2011-01-09T16:32:22-08:00Eric Pruitthttp://code.activestate.com/recipes/users/4170757/http://code.activestate.com/recipes/577518-rsync-algorithm/ <p style="color: grey"> Python recipe 577518 by <a href="/recipes/users/4170757/">Eric Pruitt</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/delta/">delta</a>, <a href="/recipes/tags/diff/">diff</a>, <a href="/recipes/tags/patch/">patch</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/rsync/">rsync</a>). Revision 4. </p> <p>This is a pure Python implementation of the <a href="http://samba.anu.edu.au/rsync/">rsync algorithm</a>. On my desktop (3.0GHz dual core, 7200RPM), best case throughput for target file hash generation and delta generation is around 2.9MB/s. Absolute worst case scenario (no blocks in common) throughput for delta generation is 200KB/s to 300KB/s on the same system.</p> <p>Tested in Python 2.5, 2.6, and 3.1. In 2.7, io.BufferedReader should yield the best throughput. On all other versions use __builtin__.open.</p> Yet another 'enum' for Python (Python) 2010-01-28T18:23:11-08:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/577024-yet-another-enum-for-python/ <p style="color: grey"> Python recipe 577024 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/enum/">enum</a>). Revision 5. </p> <p>There are quite a few 'enum' recipes already here. This one is short, cheap, and has neither bells nor whistles :)</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> Win Services helper (Python) 2014-09-06T10:35:54-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/551780-win-services-helper/ <p style="color: grey"> Python recipe 551780 by <a href="/recipes/users/4035877/">Louis RIVIERE</a> (<a href="/recipes/tags/services/">services</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 4. </p> <p>A simple way to implement Windows Service.</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> Caller and Callee (Python) 2010-03-26T20:04:11-07:00Michael Grünewaldhttp://code.activestate.com/recipes/users/4172244/http://code.activestate.com/recipes/576925-caller-and-callee/ <p style="color: grey"> Python recipe 576925 by <a href="/recipes/users/4172244/">Michael Grünewald</a> (<a href="/recipes/tags/callee/">callee</a>, <a href="/recipes/tags/caller/">caller</a>, <a href="/recipes/tags/debugging/">debugging</a>). Revision 4. </p> <p>A recipe to find out which function is the caller of the current function. </p> <p>The <code>caller</code> function can be helpful for debugging &mdash; if there is no real debugger available. In terms of software engineering (loose coupling etc.) this should not be used in production code though.</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>