Popular recipes by Romain Dartigues http://code.activestate.com/recipes/users/4167472/popular/2013-09-07T10:24:17-07:00ActiveState Code Recipesnaive natural sort (Python) 2011-08-13T16:47:36-07:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577679-naive-natural-sort/ <p style="color: grey"> Python recipe 577679 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>I wrote this after reading The Alphanum Algorithm (<a href="http://www.davekoelle.com/alphanum.html" rel="nofollow">http://www.davekoelle.com/alphanum.html</a>) by David Koelle a few years ago; my goal was to improve the performances of the Python version of his scripts.</p> <p>My version is approximatly 10 times faster than it's <code>alphanum.py</code> and about 3 times faster than the <code>alphanum.py_v2.4</code> on my computer, yielding the same results (for non-unicode at least).</p> <p><strong>Note</strong>: see the version of wizkid in the comments which is even faster.</p> scale rectangle while keeping aspect ratio (Python) 2011-02-20T22:36:39-08:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577575-scale-rectangle-while-keeping-aspect-ratio/ <p style="color: grey"> Python recipe 577575 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/resize/">resize</a>, <a href="/recipes/tags/simple/">simple</a>). Revision 2. </p> <p>Resize a rectangle (ie.: an image) while keeping the aspect ratio, without float.</p> <p>You can scale up and down the proportions so the edges will be either <strong>at most</strong> or <strong>at least</strong> the given one.</p> interleave bits (aka morton-ize aka z-order curve) (Python) 2011-02-02T12:50:58-08:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577558-interleave-bits-aka-morton-ize-aka-z-order-curve/ <p style="color: grey"> Python recipe 577558 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/bits/">bits</a>, <a href="/recipes/tags/manipulation/">manipulation</a>, <a href="/recipes/tags/mathematical/">mathematical</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>This recipe let you encode in a single number two or three numbers.</p> <p>Note: this is only an adaptation of the recipes from <strong>Sean Eron Anderson</strong> and <strong>Fabian “ryg” Giesen</strong>; all credits goes to the respective authors.</p> <ul> <li><a href="http://graphics.stanford.edu/%7Eseander/bithacks.html#InterleaveBMN" rel="nofollow">http://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN</a></li> <li><a href="http://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/" rel="nofollow">http://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/</a></li> </ul> hexadecimal CSS colors, convert the six-digit to the three-digit notation (Bash) 2010-10-02T14:07:23-07:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577416-hexadecimal-css-colors-convert-the-six-digit-to-th/ <p style="color: grey"> Bash recipe 577416 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/css/">css</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/rgb/">rgb</a>, <a href="/recipes/tags/sed/">sed</a>, <a href="/recipes/tags/shortening/">shortening</a>). </p> <p>Want to replace all six-digit RGB notation (#rrggbb) to the short (three-digit #rgb) form? Or the opposite?</p> Decimal and binary prefix Python snippet (Python) 2010-05-06T17:09:44-07:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577220-decimal-and-binary-prefix-python-snippet/ <p style="color: grey"> Python recipe 577220 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/prefix/">prefix</a>). </p> <p>Really short snipped of a decimal and binary prefix calculator in Python.</p> <p>Without covering the entire International System of Units (SI) range, it does fit my daily needs. <br /> The code could be <em>way</em> smarter, but i wanted to keep the recipe as basic as i could.</p> <p>Tested on <code>Python 2.4.1</code> and <code>2.6.4;</code> could work on lower as well.</p> extract emails from a mbox read on stdin (Python) 2013-09-07T10:24:17-07:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/576553-extract-emails-from-a-mbox-read-on-stdin/ <p style="color: grey"> Python recipe 576553 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/mbox/">mbox</a>, <a href="/recipes/tags/read_only/">read_only</a>, <a href="/recipes/tags/stdin/">stdin</a>). Revision 2. </p> <p>The Python <code>mailbox.mbox</code> class require a real file to initialize, which was an issue in my case. These simple functions let you iter through a mailbox read from a read-only file descriptor (like <code>sys.stdin</code>).</p> <p>This script use the generators which were introduced in Python-2.2. Let me know if you are interested a similar functionnality on older Python versions.</p>