Popular recipes tagged "meta:score=5"http://code.activestate.com/recipes/tags/meta:score=5/2016-04-28T13:21:43-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>
Convert CSV to XML (Python)
2010-10-11T06:20:19-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577423-convert-csv-to-xml/
<p style="color: grey">
Python
recipe 577423
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/xml/">xml</a>).
</p>
<p>Convert CSV to XML.</p>
wrist friendly dictionary (Python)
2013-08-18T22:54:26-07:00Ariel Keselmanhttp://code.activestate.com/recipes/users/4187559/http://code.activestate.com/recipes/578644-wrist-friendly-dictionary/
<p style="color: grey">
Python
recipe 578644
by <a href="/recipes/users/4187559/">Ariel Keselman</a>
(<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/nested/">nested</a>).
</p>
<p>this dictionary allows easy manual creation of nested hierarchies, like so:</p>
<p>window.style.width=5</p>
<p>or... </p>
<p>window['background-color'].rgb= 255,255,255</p>
Wrap any iterable context manager so it closes when consumed (Python)
2012-11-19T20:10:35-08:00Andrew Barnerthttp://code.activestate.com/recipes/users/4184316/http://code.activestate.com/recipes/578342-wrap-any-iterable-context-manager-so-it-closes-whe/
<p style="color: grey">
Python
recipe 578342
by <a href="/recipes/users/4184316/">Andrew Barnert</a>
(<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/with_statement/">with_statement</a>).
</p>
<p>There are a few types in Python—most notably, files—that are both iterators and context managers. For trivial cases, these features are easy to use together, but as soon as you need to use the iterator lazily or asynchronously, a with statement won't help. That's where this recipe comes in handy:</p>
<pre class="prettyprint"><code>send_async(with_iter(open(path, 'r')))
</code></pre>
<p>This also allows you to "forward" closing for a wrapped iterator, so closing the outer iterator also closes the inner one:</p>
<pre class="prettyprint"><code>sync_async(line.upper() for line in with_iter(open(path, 'r')))
</code></pre>
download the Activestate cook book recipe (Python)
2013-01-29T16:24:22-08:00lwz7512http://code.activestate.com/recipes/users/4185066/http://code.activestate.com/recipes/578439-download-the-activestate-cook-book-recipe/
<p style="color: grey">
Python
recipe 578439
by <a href="/recipes/users/4185066/">lwz7512</a>
.
</p>
<p>Small effort to store the python recipes to our local</p>
<p>similar effort by other people:
543267-i-will-download-all-of-the-recipes-from-the-python
535162-i-download-all-the-python-cookbook-recipes</p>
One-liner to show the caller of the current function. (Python)
2013-02-12T13:13:16-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578422-one-liner-to-show-the-caller-of-the-current-functi/
<p style="color: grey">
Python
recipe 578422
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/_getframe/">_getframe</a>).
</p>
<p>This is a quick one-liner to produce the filename and line number of the caller of the current function.</p>
Python code clone detector (Don't Repeat Yourself) (Python)
2012-07-12T14:59:11-07:00frahttp://code.activestate.com/recipes/users/4182629/http://code.activestate.com/recipes/578206-python-code-clone-detector-dont-repeat-yourself/
<p style="color: grey">
Python
recipe 578206
by <a href="/recipes/users/4182629/">fra</a>
(<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/clone/">clone</a>, <a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/dry/">dry</a>, <a href="/recipes/tags/duplication/">duplication</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/similarity/">similarity</a>, <a href="/recipes/tags/static/">static</a>, <a href="/recipes/tags/syntax/">syntax</a>).
Revision 2.
</p>
<p>Find duplicate code in Python 2/3 source files. Write a nice report about it.</p>
<p>Works at the Abstract Syntax Tree level, which is a robust way to detect clones.
See this <a href="http://francois.boutines.free.fr/python-3.2-report.html">code duplicated in the Python 3.2 standard library</a>.</p>
<p><strong>Update</strong>: I cleaned the code a little bit, made it Python 2.7 compatible and faster.</p>
Recursively walk Python objects (Python)
2011-12-23T22:10:38-08:00Yaniv Akninhttp://code.activestate.com/recipes/users/4180246/http://code.activestate.com/recipes/577982-recursively-walk-python-objects/
<p style="color: grey">
Python
recipe 577982
by <a href="/recipes/users/4180246/">Yaniv Aknin</a>
(<a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/recursive_iterator/">recursive_iterator</a>, <a href="/recipes/tags/walk/">walk</a>).
Revision 2.
</p>
<p>A small function that walks over pretty much any Python object and yields the objects contained within (if any) along with the path to reach them. I wrote it and am using it to validate a deserialized data-structure, but you can probably use it for many things.</p>
Dropbox file uploader via web interface using Python with urllib2 and mechanize (Python)
2016-04-28T13:21:43-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578030-dropbox-file-uploader-via-web-interface-using-pyth/
<p style="color: grey">
Python
recipe 578030
by <a href="/recipes/users/4170754/">ccpizza</a>
(<a href="/recipes/tags/dropbox/">dropbox</a>, <a href="/recipes/tags/mechanize/">mechanize</a>).
Revision 7.
</p>
<p>UPDATE:
This is script is not maintained and does not anymore with the current version of Dropbox. For a proper command line interface to dropbox I recommend <code>dropbox_uploader</code>: <a href="https://github.com/andreafabrizi/Dropbox-Uploader" rel="nofollow">https://github.com/andreafabrizi/Dropbox-Uploader</a></p>
<p>Originally inspired by the example at <a href="http://joncraton.org/blog/62/uploading-dropbox-python" rel="nofollow">http://joncraton.org/blog/62/uploading-dropbox-python</a>. </p>
<p>The script uses mechanize to logon to the web page and upload the file(s) to the Dropbox root folder or to the folder supplied on the command line as <code>dir:/my_dropbox_path</code> (if present, this must be the first parameter).</p>
<p>Multiple files and/or glob patterns names are accepted as script arguments.</p>
<h5 id="example-usage">Example usage</h5>
<pre class="prettyprint"><code>dropbox.py file1.txt # upload to root folder
dropbox.py dir:/Backups/2012 file1.txt # upload to custom folder
dropbox.py dir:/Backups/2012 *.txt # upload by file mask
dropbox.py dir:/Backups/2020 * # upload all files in current dir
</code></pre>
<p>Limitations: only files in current folder are processed, subfolders are ignored.</p>
<h5 id="note">NOTE</h5>
<p>The script requires the <code>mechanize</code> module - use <code>pip install mechanize</code> or <code>easy_install mechanize</code> to add it to your site-packages.</p>
<h5 id="note2">NOTE2</h5>
<p>I have found a cleaner way to manage dropbox files from the console - see the <em>dropbox-uploade</em>r script at <a href="https://github.com/andreafabrizi/Dropbox-Uploader" rel="nofollow">https://github.com/andreafabrizi/Dropbox-Uploader</a> - it is a Bash script that works using the official Dropbox API rather than emulating a web browser.</p>
ActiveState recipe importer (Python)
2011-11-23T02:27:51-08:00nosklohttp://code.activestate.com/recipes/users/4166478/http://code.activestate.com/recipes/577958-activestate-recipe-importer/
<p style="color: grey">
Python
recipe 577958
by <a href="/recipes/users/4166478/">nosklo</a>
(<a href="/recipes/tags/activestate/">activestate</a>, <a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/library/">library</a>).
</p>
<p>Finally! This code allows you to import any activestate recipe right into your code!</p>
<p>Example:</p>
<pre class="prettyprint"><code>>>> from activestate.recipe194373 import mreplace
>>> print mreplace('ectave steta racipas rock!', ('a', 'e'), ('e', 'a'))
active state recipes rock!
</code></pre>
<p>Save this as <strong>activestate.py</strong></p>
Left-handed password generator (Python)
2011-10-31T12:51:14-07:00Alexhttp://code.activestate.com/recipes/users/4179771/http://code.activestate.com/recipes/577930-left-handed-password-generator/
<p style="color: grey">
Python
recipe 577930
by <a href="/recipes/users/4179771/">Alex</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/lefthand/">lefthand</a>, <a href="/recipes/tags/lefthanded/">lefthanded</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/passwords/">passwords</a>).
Revision 2.
</p>
<p>Generate passwords that can easily be typed with only the left hand, very simple script</p>
Discrete PID Controller (Python)
2015-12-18T20:59:43-08:00Canerhttp://code.activestate.com/recipes/users/4114638/http://code.activestate.com/recipes/577231-discrete-pid-controller/
<p style="color: grey">
Python
recipe 577231
by <a href="/recipes/users/4114638/">Caner</a>
(<a href="/recipes/tags/controller/">controller</a>, <a href="/recipes/tags/derivative/">derivative</a>, <a href="/recipes/tags/discrete/">discrete</a>, <a href="/recipes/tags/integral/">integral</a>, <a href="/recipes/tags/pid/">pid</a>, <a href="/recipes/tags/proportional/">proportional</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>The recipe gives simple implementation of a Discrete Proportional-Integral-Derivative (PID) controller. PID controller gives output value for error between desired reference input and measurement feedback to minimize error value.
More information: <a href="http://en.wikipedia.org/wiki/PID_controller" rel="nofollow">http://en.wikipedia.org/wiki/PID_controller</a></p>
<p><strong>For new version please check:</strong> <a href="https://github.com/ivmech/ivPID" rel="nofollow">https://github.com/ivmech/ivPID</a></p>
Sleepsort (Python)
2011-06-17T05:17:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577756-sleepsort/
<p style="color: grey">
Python
recipe 577756
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 2.
</p>
<p>Funky sort routine to demonstrate Python's threading basics.</p>
Efficient Running Median using an Indexable Skiplist (Python)
2010-02-07T14:40:03-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576930-efficient-running-median-using-an-indexable-skipli/
<p style="color: grey">
Python
recipe 576930
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/advanced_data_structure/">advanced_data_structure</a>, <a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/new_algorithm/">new_algorithm</a>, <a href="/recipes/tags/skiplist/">skiplist</a>, <a href="/recipes/tags/sliding_window/">sliding_window</a>).
Revision 10.
</p>
<p>Maintains sorted data as new elements are added and old one removed as a sliding window advances over a stream of data. Also gives fast indexed access to value. Running time per median update is proportional to the log of the window size.</p>
Check for package updates on PyPI (works best in pip+virtualenv) (Python)
2011-05-19T17:54:43-07:00Artur Siekielskihttp://code.activestate.com/recipes/users/4177664/http://code.activestate.com/recipes/577708-check-for-package-updates-on-pypi-works-best-in-pi/
<p style="color: grey">
Python
recipe 577708
by <a href="/recipes/users/4177664/">Artur Siekielski</a>
(<a href="/recipes/tags/pip/">pip</a>, <a href="/recipes/tags/pypi/">pypi</a>, <a href="/recipes/tags/virtualenv/">virtualenv</a>).
</p>
<p>Pip has an option to upgrade a package (_pip install -U_), however it always downloads sources even if there is already a newest version installed. If you want to check updates for all installed packages then some scripting is required.</p>
<p>This script checks if there is a newer version on PyPI for every installed package. It only prints information about available version and doesn't do any updates. Example output:</p>
<pre class="prettyprint"><code>distribute 0.6.15 0.6.16 available
Baker 1.1 up to date
Django 1.3 up to date
ipython 0.10.2 up to date
gunicorn 0.12.1 0.12.2 available
pyprof2calltree 1.1.0 up to date
profilestats 1.0.2 up to date
mercurial 1.8.3 up to date
</code></pre>
Python Thread Pool (Python)
2010-04-12T22:27:32-07:00Emilio Montihttp://code.activestate.com/recipes/users/4173642/http://code.activestate.com/recipes/577187-python-thread-pool/
<p style="color: grey">
Python
recipe 577187
by <a href="/recipes/users/4173642/">Emilio Monti</a>
(<a href="/recipes/tags/pool/">pool</a>, <a href="/recipes/tags/thread/">thread</a>).
Revision 9.
</p>
<p>A simple Python ThreadPool based on the standard library's <a href="http://docs.python.org/library/queue.html">Queue</a> object.</p>
transform command line arguments to args and kwargs for a method call (Python)
2010-03-21T22:41:42-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577122-transform-command-line-arguments-to-args-and-kwarg/
<p style="color: grey">
Python
recipe 577122
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/argv/">argv</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/json/">json</a>).
Revision 2.
</p>
<p>For many of my Python modules I find it convenient to provide a small <code>if __name__ == "__main__": ...</code> block to be able to call individual methods from the command line. This requires some kind of translation of command-line string arguments to <code>args</code> and <code>kwargs</code> for the method call. This recipe uses a few conventions to do that:</p>
<ul>
<li>the first argument is the method name</li>
<li>positional args are positional (duh)</li>
<li>"key=value" is a keyword argument</li>
<li>an attempt is made to interpret arguments as JSON to allow specifying types other than string</li>
</ul>
sending gmail though python code (Python)
2013-03-23T10:01:02-07:00Raghuramhttp://code.activestate.com/recipes/users/4174757/http://code.activestate.com/recipes/577371-sending-gmail-though-python-code/
<p style="color: grey">
Python
recipe 577371
by <a href="/recipes/users/4174757/">Raghuram</a>
(<a href="/recipes/tags/python_scripts/">python_scripts</a>).
Revision 2.
</p>
<p>sending gmail though your python code</p>
Improved ReportLab recipe for "page x of y" (Python)
2009-07-06T10:03:28-07:00Vinay Sajiphttp://code.activestate.com/recipes/users/4034162/http://code.activestate.com/recipes/576832-improved-reportlab-recipe-for-page-x-of-y/
<p style="color: grey">
Python
recipe 576832
by <a href="/recipes/users/4034162/">Vinay Sajip</a>
(<a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/reportlab/">reportlab</a>).
Revision 2.
</p>
<p>This recipe is based on <a href="http://code.activestate.com/recipes/546511/"><a href="http://code.activestate.com/recipes/546511/">Recipe 546511</a></a> which does not work reliably if there are images in the content.</p>
Counter class (Python)
2009-06-29T12:24:14-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576611-counter-class/
<p style="color: grey">
Python
recipe 576611
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
.
Revision 11.
</p>
<p>Bag/multiset class for convenient tallying of hashable objects.</p>