Popular recipes tagged "meta:loc=22"http://code.activestate.com/recipes/tags/meta:loc=22/2017-01-11T10:10:30-08:00ActiveState Code RecipesRetry Decorator in Python (Python)
2017-01-11T10:10:30-08:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580745-retry-decorator-in-python/
<p style="color: grey">
Python
recipe 580745
by <a href="/recipes/users/4182236/">Alfe</a>
(<a href="/recipes/tags/aspect/">aspect</a>, <a href="/recipes/tags/except/">except</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/retry/">retry</a>, <a href="/recipes/tags/try/">try</a>).
</p>
<p>This is a Python decorator which helps implementing an aspect oriented implementation of a <em>retrying</em> of certain steps which might fail sometimes. A typical example for this would be communication processes with the outside world, e. g. HTTP requests, allocation of some resource, etc. To use it, refactor the step in question into a (local) function and decorate this with the <code>retry</code> decorator. See examples in the discussion sector below.</p>
Fast 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>
FileSpec: Set it, forget it, reuse it (Python)
2016-03-08T05:49:16-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/580618-filespec-set-it-forget-it-reuse-it/
<p style="color: grey">
Python
recipe 580618
by <a href="/recipes/users/4076953/">Jack Trainor</a>
(<a href="/recipes/tags/filename/">filename</a>, <a href="/recipes/tags/utilities/">utilities</a>).
</p>
<p>Python provides good utilities for transforming filenames, but they are tedious to use and clutter up the source code.</p>
<p>FileSpec offers one-stop shopping to convert a file path to every component you might want to know, reuse, or transform.</p>
high precision FPS (Python)
2015-05-12T12:04:38-07:00Jiri Justrahttp://code.activestate.com/recipes/users/4192188/http://code.activestate.com/recipes/579053-high-precision-fps/
<p style="color: grey">
Python
recipe 579053
by <a href="/recipes/users/4192188/">Jiri Justra</a>
(<a href="/recipes/tags/fps/">fps</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/games/">games</a>, <a href="/recipes/tags/pure/">pure</a>).
Revision 2.
</p>
<p>This code adjust itself for set FPS value. It is much more precise that time.sleep fps implementation.</p>
Method chaining or cascading (Python)
2016-09-01T12:34:17-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578770-method-chaining-or-cascading/
<p style="color: grey">
Python
recipe 578770
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/cascade/">cascade</a>, <a href="/recipes/tags/cascading/">cascading</a>, <a href="/recipes/tags/chaining/">chaining</a>, <a href="/recipes/tags/method/">method</a>).
</p>
<p>A frequently missed feature of built-ins like lists and dicts is the ability to chain method calls like this:</p>
<pre class="prettyprint"><code>x = []
x.append(1).append(2).append(3).reverse().append(4)
# x now equals [3, 2, 1, 4]
</code></pre>
<p>Unfortunately this doesn't work, as mutator methods return <code>None</code> rather than <code>self</code>. One possibility is to design your class from the beginning with method chaining in mind, but what do you do with those like the built-ins which aren't?</p>
<p>This is sometimes called <a href="https://en.wikipedia.org/wiki/Method_cascading">method cascading</a>. Here's a proof-of-concept for an adapter class which turns any object into one with methods that can be chained.</p>
A simple shell script to keep the wife off of your back... (Bash)
2013-12-09T20:05:49-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578781-a-simple-shell-script-to-keep-the-wife-off-of-your/
<p style="color: grey">
Bash
recipe 578781
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/reminder/">reminder</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/shell/">shell</a>).
</p>
<p>How many times have you been asked to remember to do something from the other half whilst she is out for a short while.</p>
<p>For example: "You WILL check the dinner every few minutes won't you?"</p>
<p>And how many times did/do you forget?</p>
<p>Most of us have been there...</p>
<p>This is a simple kids level, practical learning, shell script that generates an "xterm" with your reminder inside every 30 seconds for a period of 3 seconds.</p>
<p>It is always be the active front window for 3 seconds at a time to _annoy_ you into remembering.</p>
<p>Usage: reminder "What you have to remember here using spaces AND double quotes."<CR></p>
<p>Just reanme the downloaded script to reminder and remember to chmod it as required.</p>
<p>Just run it from your default terminal and when finished press Ctrl-C just AFTER the xterm window closes.</p>
<p>There is NO error detection so steer clear of any special characters in you reminder text.</p>
<p>Enjoy finding simple solutions to often very difficult problems...</p>
Fixed Point Iteration in Python (Python)
2013-01-16T16:39:08-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578418-fixed-point-iteration-in-python/
<p style="color: grey">
Python
recipe 578418
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/fixed_point/">fixed_point</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>The code utilizes fixed point iteration to solve equations in python. This code was wrriten for <a href="http://thelivingpearl.com/2013/01/15/the-easy-way-to-solve-equations-in-python/">How to solve equations using python</a>.</p>
Multiple unique class instances - a tentative design pattern (Python)
2013-02-23T14:11:44-08:00Moritz Beberhttp://code.activestate.com/recipes/users/4185350/http://code.activestate.com/recipes/578471-multiple-unique-class-instances-a-tentative-design/
<p style="color: grey">
Python
recipe 578471
by <a href="/recipes/users/4185350/">Moritz Beber</a>
(<a href="/recipes/tags/design_pattern/">design_pattern</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/unique/">unique</a>).
</p>
<p>Coming from Bioinformatics and having to deal with multiple objects with unique properties, like genes, proteins, and many more, I felt the need for a class that would always yield a unique instance based on a chosen identifier. This is because I always wanted the same instances whose attributes were filled with information and track them in various storage classes, like dictionaries, lists, etc. The code for the class lives <a href="https://github.com/Midnighter/unique-base">over on github</a>. Recently, I've added a lot of parallel-processing code so I adapted the pickling behaviour for this class to also yield existing instances. What follows is an example of how to use it and some discussion questions.</p>
LXML Etree XML Object to basic Python {dict+lists} (Python)
2012-08-20T07:06:54-07:00Luis Martin Gilhttp://code.activestate.com/recipes/users/4183220/http://code.activestate.com/recipes/578244-lxml-etree-xml-object-to-basic-python-dictlists/
<p style="color: grey">
Python
recipe 578244
by <a href="/recipes/users/4183220/">Luis Martin Gil</a>
(<a href="/recipes/tags/lxml/">lxml</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/xml/">xml</a>).
</p>
<p>Convert an Etree XML structure into a Python dict+list</p>
Disable File System Redirector (Python)
2012-02-06T16:34:02-08:00zxwhttp://code.activestate.com/recipes/users/4180790/http://code.activestate.com/recipes/578035-disable-file-system-redirector/
<p style="color: grey">
Python
recipe 578035
by <a href="/recipes/users/4180790/">zxw</a>
(<a href="/recipes/tags/32bit/">32bit</a>, <a href="/recipes/tags/64bit/">64bit</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/redirector/">redirector</a>, <a href="/recipes/tags/windows/">windows</a>).
Revision 5.
</p>
<p>This disables the <a href="http://msdn.microsoft.com/en-us/library/aa384187(v=vs.85).aspx">Windows File System Redirector</a>.</p>
<p>When a 32 bit program runs on a 64 bit operating system the paths to C:/Windows/System32 automatically get redirected to the 32 bit version (C:/Windows/SysWow64), if you really do need to access the contents of System32, you need to disable the file system redirector first.</p>
RPDB (RobotPythonDebugger) -- a smarter way to debug robotframework tests (Python)
2012-03-14T15:12:39-07:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/578073-rpdb-robotpythondebugger-a-smarter-way-to-debug-ro/
<p style="color: grey">
Python
recipe 578073
by <a href="/recipes/users/4172918/">Daniel Cohn</a>
(<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/pdb/">pdb</a>, <a href="/recipes/tags/redirect/">redirect</a>, <a href="/recipes/tags/robot/">robot</a>, <a href="/recipes/tags/rpdb/">rpdb</a>, <a href="/recipes/tags/stdin/">stdin</a>, <a href="/recipes/tags/stdout/">stdout</a>).
</p>
<p>Robotframework (<a href="http://code.google.com/p/robotframework/" rel="nofollow">http://code.google.com/p/robotframework/</a>) is a tool used to run functional tests against a variety of targets. Tests are organized in the form of keyword tsv or html files, which map input parameters to keyword-argument methods in the test suite. Robot includes a fairly advanced logging mechanism, which is cool -- until you try to debug anything. Debugging is made difficult because robot steals stdin and stdout when it is run, which means bye-bye debugging in the terminal. rpdb solves this in a KISS simple way.</p>
Show all the telecommuting jobs from the Python Job Board (Python)
2011-12-09T07:38:28-08:00Victor Yanghttp://code.activestate.com/recipes/users/627255/http://code.activestate.com/recipes/577979-show-all-the-telecommuting-jobs-from-the-python-jo/
<p style="color: grey">
Python
recipe 577979
by <a href="/recipes/users/627255/">Victor Yang</a>
(<a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/screenscrape/">screenscrape</a>).
</p>
<p>It is running as a cronjob on a VPS(Virutal Private Server). The output html can be served by any web server. </p>
Class decorator to check that methods are implemented. (Python)
2011-05-27T23:52:28-07:00Jeffrey Fischerhttp://code.activestate.com/recipes/users/4178137/http://code.activestate.com/recipes/577725-class-decorator-to-check-that-methods-are-implemen/
<p style="color: grey">
Python
recipe 577725
by <a href="/recipes/users/4178137/">Jeffrey Fischer</a>
(<a href="/recipes/tags/decorators/">decorators</a>).
Revision 2.
</p>
<p>This decorator wraps the class's __init__ function to check that provided set of methods is present on the instantiated class. This is useful for avoiding inheritance in mix-in classes. We can inherit directly from object and still make it clear what methods we expect in other classes to be combined with the mix-in.</p>
<p>Requires at least Python 2.6, which added class decorators.</p>
Setting a file descriptor to blocking or non-blocking mode (C)
2010-09-06T11:20:53-07:00Emilio Montihttp://code.activestate.com/recipes/users/4173642/http://code.activestate.com/recipes/577384-setting-a-file-descriptor-to-blocking-or-non-block/
<p style="color: grey">
C
recipe 577384
by <a href="/recipes/users/4173642/">Emilio Monti</a>
(<a href="/recipes/tags/blocking/">blocking</a>, <a href="/recipes/tags/file_descriptor/">file_descriptor</a>, <a href="/recipes/tags/non_blocking/">non_blocking</a>).
Revision 2.
</p>
<p>A simple function to set a file descriptor (i.e. a socket) to blocking or non-blocking mode.</p>
Calculate Pi using Monte Carlo Simulations in Python (Vectorized) (Python)
2011-02-17T22:35:46-08:00Zach Pacehttp://code.activestate.com/recipes/users/4177048/http://code.activestate.com/recipes/577578-calculate-pi-using-monte-carlo-simulations-in-pyth/
<p style="color: grey">
Python
recipe 577578
by <a href="/recipes/users/4177048/">Zach Pace</a>
(<a href="/recipes/tags/calculate/">calculate</a>, <a href="/recipes/tags/carlo/">carlo</a>, <a href="/recipes/tags/monte/">monte</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>I saw something like this in C++ as an introduction to Monte Carlo, so I decided to make something similar in Python. My original code used for loops, but I vectorized it with no small amount of effort, and it now runs orders of magnitude faster. For example, I can calculate pi to .002% error with 100,000,000 randomized coordinates in approximately 15 seconds. Careful to start small, because memory fills up quickly, and the computer will run slow if you overstep your RAM. I'm able to go up to a bit more than 150 million without compromising speed and functionality in other tasks.</p>
<p>For those who are curious, vectorization is a technique whereby numpy (or similar) arrays replace things like loops. They're a bit tricky to write (at least for me), but they work beautifully.</p>
<p>It might be useful for visualization to plot the occurrence of data points, and observe the randomness</p>
Simplest example of serving a browser request (Python)
2010-10-15T21:17:30-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577428-simplest-example-of-serving-a-browser-request/
<p style="color: grey">
Python
recipe 577428
by <a href="/recipes/users/4166965/">Kaushik Ghose</a>
(<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/webserver/">webserver</a>).
</p>
<p>This is the simplest way I have found of sending information from a python program to a browser</p>
Rename non-ASCII filenames to readable ASCII, i.e. replace accented characters, etc (Python)
2011-11-03T17:50:55-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577226-rename-non-ascii-filenames-to-readable-ascii-ie-re/
<p style="color: grey">
Python
recipe 577226
by <a href="/recipes/users/4170754/">ccpizza</a>
(<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/renaming/">renaming</a>, <a href="/recipes/tags/unicode/">unicode</a>).
Revision 3.
</p>
<p>The script converts any accented characters in filenames to their ASCII equivalents. e.g.:</p>
<p>Example:</p>
<pre class="prettyprint"><code>â > a
ä > a
à > a
á > a
é > e
í > i
ó > o
ú > u
ñ > n
ü > u
...
</code></pre>
<p>Before-and-after example:</p>
<pre class="prettyprint"><code>01_Antonín_Dvořák_Allegro.mp3 >>> 01_Antonin_Dvorak_Allegro.mp3
</code></pre>
<p>Usage:</p>
<pre class="prettyprint"><code>Running the script without arguments will rename all files in the current folder.
!!!WARNING!!! ***No*** backups are created.
</code></pre>
Create module dependency graph (Python)
2010-05-07T11:29:03-07:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577222-create-module-dependency-graph/
<p style="color: grey">
Python
recipe 577222
by <a href="/recipes/users/4173873/">Noufal Ibrahim</a>
(<a href="/recipes/tags/dependecies/">dependecies</a>, <a href="/recipes/tags/graphs/">graphs</a>, <a href="/recipes/tags/utilities/">utilities</a>).
</p>
<p>The following snippet will dump the module dependencies in a format that can be interpreted by the dot program distributed along with graphviz. You can use it like below to see the dependency graph for the asynchat module for example. (the program is saved as grapher.py)</p>
<pre class="prettyprint"><code>python grapher.py asynchat | dot -Tpng | display
</code></pre>
<p>A screenshot is available here <a href="http://twitpic.com/1lqnmh" rel="nofollow">http://twitpic.com/1lqnmh</a></p>
Percolation Cluster Fractals using PIL Floodfill (Python)
2010-03-25T14:02:25-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577141-percolation-cluster-fractals-using-pil-floodfill/
<p style="color: grey">
Python
recipe 577141
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>You can change the average size of the clusters by changing the maxIt value.
(The calculation would take longer if you increase it though.)</p>
Copy audio files from exported playlist of iTunes (using plistlib) (Python)
2010-03-13T19:08:30-08:00Keisuke URAGOhttp://code.activestate.com/recipes/users/668964/http://code.activestate.com/recipes/577107-copy-audio-files-from-exported-playlist-of-itunes-/
<p style="color: grey">
Python
recipe 577107
by <a href="/recipes/users/668964/">Keisuke URAGO</a>
(<a href="/recipes/tags/itunes/">itunes</a>).
Revision 3.
</p>
<p>It script is copy from original audio file to directory of you decide.</p>