Popular recipes by yota http://code.activestate.com/recipes/users/4184815/2016-04-21T14:12:33-07:00ActiveState Code Recipesnbitarray (Python)
2016-04-21T14:12:33-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/580649-nbitarray/
<p style="color: grey">
Python
recipe 580649
by <a href="/recipes/users/4184815/">yota</a>
.
</p>
<p>library to store n bits data (n > 0) in a python array</p>
ed25519 (Python)
2015-09-21T12:58:34-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/579102-ed25519/
<p style="color: grey">
Python
recipe 579102
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/cryptography/">cryptography</a>).
Revision 2.
</p>
<p>This is a re-implementation of the ed25519 signature algorithm as proposed on this page : <a href="http://ed25519.cr.yp.to/python/ed25519.py." rel="nofollow">http://ed25519.cr.yp.to/python/ed25519.py.</a></p>
<p>Do not use for production, only for the eyes o_O</p>
<p>Code is tab indented, space indentation kills kitten...</p>
least square fitting (Python)
2015-10-03T15:32:28-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/579106-least-square-fitting/
<p style="color: grey">
Python
recipe 579106
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/fitting/">fitting</a>).
Revision 2.
</p>
<p>a generic python code to fit points to a given curve, was made for a paraboloid, but can be easily expanded to many kind of curves</p>
pick all combinations of items in buckets (Python)
2015-09-05T07:39:42-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/579098-pick-all-combinations-of-items-in-buckets/
<p style="color: grey">
Python
recipe 579098
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/combinatorics/">combinatorics</a>).
</p>
<p>Let be a number of buckets, containing each, a variable number of items. This function return all combinations possible of one item picked out of each bucket</p>
<p>example, with three buckets {ba, be, bi}, {ka, ko, ku, ke} and {to, ty}, the function enumerate as such: </p>
<pre class="prettyprint"><code> 0. ba-ka-to
1. ba-ka-ty
2. ba-ko-to
3. ba-ko-ty
4. ba-ku-to
5. ba-ku-ty
6. ba-ke-to
7. ba-ke-ty
8. be-ka-to
9. be-ka-ty
10. be-ko-to
11. be-ko-ty
12. be-ku-to
13. be-ku-ty
14. be-ke-to
15. be-ke-ty
16. bi-ka-to
17. bi-ka-ty
18. bi-ko-to
19. bi-ko-ty
20. bi-ku-to
21. bi-ku-ty
22. bi-ke-to
23. bi-ke-ty
</code></pre>
smart copy (Python)
2015-02-06T09:45:12-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/579020-smart-copy/
<p style="color: grey">
Python
recipe 579020
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/shutil/">shutil</a>).
</p>
<p>take a glob expression, a source directory and a destination directory to copy each files matching the glob in the appropriate directory</p>
<pre class="prettyprint"><code>glob = */*.txt
src_dir = ./a/b
dst_dir = /z/x/y
</code></pre>
<p>if the glob match a file <code>./a/b/c/foo.txt</code>, it will copy it in <code>/z/x/y/c/foo.txt</code> (and create the missing directory if needed)</p>
<p>Require Python3.4, code tab indented</p>
Compute Memory footprint of an object and its contents (Python)
2014-05-15T13:44:15-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578880-compute-memory-footprint-of-an-object-and-its-cont/
<p style="color: grey">
Python
recipe 578880
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>).
</p>
<p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p>
Levenshtein, my love (Python)
2014-01-15T09:14:30-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578810-levenshtein-my-love/
<p style="color: grey">
Python
recipe 578810
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p><em>be kind and comment, especially if you downvote</em></p>
<p><strong>levenshtein_distance()</strong> is an implementation of the iterative algorithm for the levenshtein distance (cf. <a href="http://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix%29" rel="nofollow">http://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix)</a></p>
<p><strong>levenshtein_sequence()</strong> is an attempt to retrieve one of the levenshtein paths (the one that give priority to substitution, deletion, insertion, in this order). The result is a list of tuples made of:</p>
<ol>
<li>the operation ( <code>=</code>, <code>-</code>, <code>+</code>, <code>*</code> for respectively keep, delete, insert, substitute)</li>
<li>the coordinate in the first</li>
<li>and in the second string.</li>
</ol>
<pre class="prettyprint"><code>>>> levenshtein_sequence('saturday', 'sunday')
[('=', 0, 0), ('-', 1, 0), ('-', 2, 0), ('=', 3, 1), ('*', 4, 2), ('=', 5, 3), ('=', 6, 4), ('=', 7, 5)]
>>> levenshtein_sequence('kitten', 'sitting')
[('*', 0, 0), ('=', 1, 1), ('=', 2, 2), ('=', 3, 3), ('*', 4, 4), ('=', 5, 5), ('+', 5, 6)]
</code></pre>
<p>This code is part of foreplays, in a plan I have to improve difflib with alternative SequenceMatchers \o/</p>
<p><em>/!\ tab indented, as usual.</em></p>
dynamic mathjax demo page (HTML)
2015-02-12T13:10:17-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578679-dynamic-mathjax-demo-page/
<p style="color: grey">
HTML
recipe 578679
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/latex/">latex</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathjax/">mathjax</a>).
Revision 7.
</p>
<p>Html web page to preview latex equations rendered by mathjax. <a href="http://jsfiddle.net/r82p49xx/5/">demo here</a></p>
<p>Equations are updated when shift, space or enter keys are pressed, or when the Textarea lose focus.</p>
<p>In the first block you can try inline-style math, in the second one, display-style math.</p>
heap class (Python)
2015-09-17T12:41:15-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578694-heap-class/
<p style="color: grey">
Python
recipe 578694
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/heap/">heap</a>).
Revision 2.
</p>
<p><em>heapq</em> is a nice python module, but the interface is not so clean. I found a one-liner on this <a href="http://metapython.blogspot.de/2010/10/creating-heap-class-in-one-python-line.html">blog</a>, it's as short as possible, but not really pythonic either :) Here is my contribution to a most readable heap class.</p>
find + grep (Bash)
2013-09-17T08:48:07-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578661-find-grep/
<p style="color: grey">
Bash
recipe 578661
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/zsh/">zsh</a>).
Revision 2.
</p>
<p>look for a text pattern in files defined by a pattern.
First argument is passed to the find command, second one to the grep</p>
<p>./find_n_grep.sh '*.py' 'dict'</p>
<p>more over, it sort by date.</p>
easy user input (Python)
2013-09-13T06:34:08-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578552-easy-user-input/
<p style="color: grey">
Python
recipe 578552
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/user/">user</a>).
Revision 7.
</p>
<p>Improvement over <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> and cie.</p>
<p><code>easy_input()</code> function extends the built-in <code>input()</code> function.
A question is prompted as well as some expected answers.</p>
<p>The user input can be incomplete (ie. <code>y</code> or <code>ye</code> instead of <code>yes</code>)</p>
<ul>
<li>If no list of expected answer is provided, default will be "yes/no".</li>
<li>If no default answer is provided, default will be the first expected answer.</li>
</ul>
<p>Try and see.</p>
<p>Disclaimer: written in python3, meant for *nix shell, indented with tabs</p>
<p><strong>Avoided caveat:</strong> If some expected <code>answer</code> have the same beginning, the user can not enter too few letters. Ex: <code>answer = ['continue', 'test', 'testicle']</code>, user can not input <code>t</code>, <code>te</code> or <code>tes</code> because it will be ambiguous. User can however input <code>test</code>, which is not.</p>
longest common substring (Python)
2013-03-05T19:22:31-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578465-longest-common-substring/
<p style="color: grey">
Python
recipe 578465
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/longest_common_substring/">longest_common_substring</a>).
Revision 4.
</p>
<p>Return, more than the substring itself, the position of the said substring, relative to each string passed in parameter.</p>
<p>String is a generic term. Here, it is an array, any object with __getitem__() method should work.</p>
topological sorting again (Python)
2013-03-06T19:21:11-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578406-topological-sorting-again/
<p style="color: grey">
Python
recipe 578406
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/topological/">topological</a>).
Revision 9.
</p>
<p>Topological sorting is the answer to the following question : in a direct acyclic graph, how can I pick up nodes "in order", such as upstream nodes are always before downstream ones ? Many solutions may exists, many algorithms too.</p>
<p>Alas, it seems I'm too stupid to understand already proposed recipes on topological sorting. Hopefully I do grasp the "write once, read many" concept.</p>
<p>Here, you will find a plain algorithm, optimized only for code clarity, of a topological sorting for direct acyclic graphs, implemented in python from the pseudo code found on <a href="http://en.wikipedia.org/wiki/Topological_sorting">wikipedia</a>:</p>
<pre class="prettyprint"><code>L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
insert n into L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
</code></pre>
<p>Only tested with python3.2, should work with other versions. Be careful, code indented with tabs, since space is evil è_é</p>