Popular recipes by bearophile - http://code.activestate.com/recipes/users/2403049/2008-04-18T06:40:20-07:00ActiveState Code RecipesBK-tree (Python)
2008-04-18T06:40:20-07:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/572156-bk-tree/
<p style="color: grey">
Python
recipe 572156
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Fast Levenshtein distance and BK-tree implementations in Python.</p>
Fast posmax (Python)
2008-01-27T02:48:07-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/543271-fast-posmax/
<p style="color: grey">
Python
recipe 543271
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>To quickly find the index of the first maximum item of a sequence.</p>
Heap, a heapq wrapper class (Python)
2007-03-09T13:55:05-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/502295-heap-a-heapq-wrapper-class/
<p style="color: grey">
Python
recipe 502295
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>A Heap wrapper class around the standard heapq, it helps avoid some common bugs with a useful OOP, and it manages a key parameter too.</p>
Python symbols (toy) (Python)
2007-01-22T18:40:30-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/500273-python-symbols-toy/
<p style="color: grey">
Python
recipe 500273
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>This toy module shows a way to define symbols inside functions, using a decorator. Idea derived from <a href="http://code.activestate.com/recipes/303057/">recipe 303057</a> by Shai Berger.</p>
Weighted choice (Python)
2006-10-30T13:48:55-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/498229-weighted-choice/
<p style="color: grey">
Python
recipe 498229
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>To choose among a list of objects using a specified frequency distribution.</p>
Another Odict (Python)
2006-10-11T22:29:20-07:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/498195-another-odict/
<p style="color: grey">
Python
recipe 498195
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>Yet another implementation of an Odict, a dictionary in which the <em>insertion</em> order of items is preserved.</p>
tensor and transpose (Python)
2006-09-21T15:25:44-07:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/498123-tensor-and-transpose/
<p style="color: grey">
Python
recipe 498123
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Two functions useful when you don't use a numerical library. The first one creates a tensor, hopefully in the correct way, avoiding the mutability trap.
The second one transposes a 2D matrix keeping the type of the lists/tuples used.</p>
Table splitter (Python)
2006-09-13T17:13:18-07:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/498093-table-splitter/
<p style="color: grey">
Python
recipe 498093
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>To split a textual table automatically. Improved from a comp.lang.python request.</p>
Another memoize (Python)
2006-01-21T10:19:44-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/466320-another-memoize/
<p style="color: grey">
Python
recipe 466320
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>This tries to be a safer and faster memoize decorator, it works with mutable types and with keyword args too. The code comes from many different versions found around. This code isn't much tested yet, so it must be used with care.</p>
Fast select (Python)
2006-01-21T11:33:31-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/466330-fast-select/
<p style="color: grey">
Python
recipe 466330
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>To quickly select the n-th rank ordered element of a given sequence. (It modifies the ordering of the given sequence).</p>
Graph (Python)
2006-01-21T19:17:42-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/466329-graph/
<p style="color: grey">
Python
recipe 466329
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>A directed Graph container can be useful for the collections module. This is a bare bones implementation, to show its basic API, a complete implementation is available. Many different graph implementations are possible, but I think this is flexible, fast and simple enough.</p>
Hierarchical Split (Python)
2006-02-19T13:23:14-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/440663-hierarchical-split/
<p style="color: grey">
Python
recipe 440663
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 4.
</p>
<p>To split a string more times, hierarchically.</p>
Conversion in base 2 (Python)
2005-09-06T20:06:48-07:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/440528-conversion-in-base-2/
<p style="color: grey">
Python
recipe 440528
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>To convert a given number into a string that contains the base 2 representation of the number, with its sign.</p>
Unique stable-unstable (Python)
2005-07-27T18:23:55-07:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/438599-unique-stable-unstable/
<p style="color: grey">
Python
recipe 438599
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>Python needs a unique function, this is my best try so far. It uses the faster algorithm (among five) for each situation.</p>
Right method names suggestion (Python)
2006-02-06T21:45:56-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/409000-right-method-names-suggestion/
<p style="color: grey">
Python
recipe 409000
by <a href="/recipes/users/2403049/">bearophile -</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 3.
</p>
<p>My first recipe here, it uses __getattr__ to modify the error messages given when a wrong class method is called. It shows the first five ranked most similar method names, followed by the first line of their docstrings (this is useful to see their parameters, if you use this convention in your sources), and then it raises an AttributeError. Useful from the interactive shell too.</p>