Popular Python recipes tagged "meta:requires=itertools"http://code.activestate.com/recipes/langs/python/tags/meta:requires=itertools/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> Compute Memory footprint of an object and its contents (Python) 2012-11-23T23:57:31-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577504-compute-memory-footprint-of-an-object-and-its-cont/ <p style="color: grey"> Python recipe 577504 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>). Revision 3. </p> <p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p> DBF reader and writer -- selective fields and nullreplace (Python) 2016-09-18T20:39:20-07:00Tomas Nordinhttp://code.activestate.com/recipes/users/4189558/http://code.activestate.com/recipes/580696-dbf-reader-and-writer-selective-fields-and-nullrep/ <p style="color: grey"> Python recipe 580696 by <a href="/recipes/users/4189558/">Tomas Nordin</a> (<a href="/recipes/tags/database/">database</a>). </p> <p>This fork assumes a desire for limited selection of field names. With huge files this might be necessary on some machines.</p> <p>Also, assuming that the meaning of null in a dbf file means zero might be a mistake, so the fork adds an argument nullreplace as way to choose what to replace null with. Null is sometimes used to mean missing value. This change is decoupled from the selective names feature.</p> Keyword-only arguments in Python 2.x (Python) 2015-09-22T18:16:40-07:00Carahttp://code.activestate.com/recipes/users/4191397/http://code.activestate.com/recipes/578993-keyword-only-arguments-in-python-2x/ <p style="color: grey"> Python recipe 578993 by <a href="/recipes/users/4191397/">Cara</a> (<a href="/recipes/tags/arguments/">arguments</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/keyword/">keyword</a>, <a href="/recipes/tags/keyword_only/">keyword_only</a>). Revision 10. </p> <p>This is a decorator that makes some of a function's or method's arguments into keyword-only arguments. As much as it possible, it emulates the Python 3 handling of keyword-only arguments, including messages for TypeErrors. It's compatible with Python 3 so it can be used in code bases that support both versions.</p> Easy attribute setting and pretty representation (Python) 2014-12-03T09:55:06-08:00Joakim Petterssonhttp://code.activestate.com/recipes/users/4174760/http://code.activestate.com/recipes/578973-easy-attribute-setting-and-pretty-representation/ <p style="color: grey"> Python recipe 578973 by <a href="/recipes/users/4174760/">Joakim Pettersson</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/traits/">traits</a>). Revision 2. </p> <p>Mix in one or more of these classes to avoid those tedious lines of administrative code for setting attributes and getting useful representations. </p> <p>If you inherit HasInitableAttributes, your should be able to obj = eval(repr(obj)) without loosing data.</p> <p>enthought.traits.api.HasTraits seems to mix in well also.</p> Proof-of-concept for a more space-efficient, faster-looping dictionary (Python) 2013-01-17T09:28:24-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578375-proof-of-concept-for-a-more-space-efficient-faster/ <p style="color: grey"> Python recipe 578375 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/compact/">compact</a>, <a href="/recipes/tags/dictionary/">dictionary</a>). Revision 20. </p> <p>Save space and improve iteration speed by moving the hash/key/value entries to a densely packed array keeping only a sparse array of indices. This eliminates wasted space without requiring any algorithmic changes.</p> QuadKeys Generator - useful for N'ary strings generation (Python) 2014-06-12T10:29:35-07:00Narayana Chikkamhttp://code.activestate.com/recipes/users/4174427/http://code.activestate.com/recipes/578888-quadkeys-generator-useful-for-nary-strings-generat/ <p style="color: grey"> Python recipe 578888 by <a href="/recipes/users/4174427/">Narayana Chikkam</a> (<a href="/recipes/tags/quinary/">quinary</a>). </p> <p>As per the standard articles available on the internet, world map could be divided into four section top left, top right, bottom left and bottom right. if we associate numbers to identify them, for eg: 0 -&gt; top left 1 -&gt; top right 2 -&gt; bottom left 3 -&gt; bottom right it would be easier to dig into a tile or a section of the map using this system. A good documentation on the same is available on the 'Bing Map Tile System'. It would be a good idea to have some snippet of code that generates these sequence of numbers in this system so that world map could be traversed in a serial fashion. The class has 3 generator methods that produce the same sequence. Width corresponds to the zoom level.</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> Running median, mean and mode (Python) 2013-03-06T17:33:32-08:00Virgil Stokeshttp://code.activestate.com/recipes/users/4183795/http://code.activestate.com/recipes/578480-running-median-mean-and-mode/ <p style="color: grey"> Python recipe 578480 by <a href="/recipes/users/4183795/">Virgil Stokes</a> (<a href="/recipes/tags/mean/">mean</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/mode/">mode</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/running/">running</a>). </p> <p>The following is a small contribution that I hope can be useful to Python programmers for the calculation of the running median, mean and mode. It is important to note that all the "running" calculations are done for full windows. Here is a simple example:</p> <p>y = [1, 2, 3, 3, 1, 4], with a sliding window of size = 3 for the running estimations, means = 2, 2.6667, 2.3333, 2.6667 medians = 2, 3, 3, 3 modes = 1, 3, 3, 1</p> Generates tuples of integers with a given sum (Python) 2013-07-16T14:03:08-07:00SnarkTurnehttp://code.activestate.com/recipes/users/4185559/http://code.activestate.com/recipes/578608-generates-tuples-of-integers-with-a-given-sum/ <p style="color: grey"> Python recipe 578608 by <a href="/recipes/users/4185559/">SnarkTurne</a> . Revision 3. </p> <p>This is a generator that generates all tuples of n integers (&gt;=0) with a given sum s. The generator can make the difference between tuples (3,0,0) and (0,3,0) or not (see order parameter). </p> Truth Value Aware Iterable (Python) 2013-06-11T08:00:25-07:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/578549-truth-value-aware-iterable/ <p style="color: grey"> Python recipe 578549 by <a href="/recipes/users/4169882/">Alan Franzoni</a> (<a href="/recipes/tags/boolean/">boolean</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/truth/">truth</a>). </p> <p>This small recipe enables truth value testing on iterables.</p> <p>It is quite common to do things like:</p> <pre class="prettyprint"><code>if somesequence: ... else: ... </code></pre> <p>Such constructs, that enter the if block if the sequence's got one or more elements and the else block if it's empty, work fine on non-lazy builtin sequences (lists, strings, tuples) and dictionaries as well, but doesn't necessarily work on generic iterables - most of them are always true regardless of their contents, since they're some kind of object. A classical example is generators, but such behaviour can be extended to any object implementing the Iterable interface.</p> <p>Just wrap your iterable with this decorator and you'll get a truth-aware iterable which supports proper truth testing by doing a small first element prefetching and can then be used just like the original iterable.</p> Create a temporary mailbox (Python) 2013-04-29T14:04:20-07:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/578514-create-a-temporary-mailbox/ <p style="color: grey"> Python recipe 578514 by <a href="/recipes/users/4173873/">Noufal Ibrahim</a> (<a href="/recipes/tags/mail/">mail</a>, <a href="/recipes/tags/mbox/">mbox</a>). </p> <p>Generates a mailbox with lots of messages.</p> Sending Email in Python (Python) 2013-02-24T13:47:01-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578472-sending-email-in-python/ <p style="color: grey"> Python recipe 578472 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/email_sending/">email_sending</a>, <a href="/recipes/tags/sendmail/">sendmail</a>, <a href="/recipes/tags/smtp/">smtp</a>). </p> <p>Every Python Application needs to send email at some point. Whether it's for reporting errors, status updates or simply the core functionality of the system this little recipe should help! Documented and Tested.</p> Password Generator (mkpasswd) (Python) 2013-07-31T23:23:02-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578468-password-generator-mkpasswd/ <p style="color: grey"> Python recipe 578468 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/mkpasswd/">mkpasswd</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/secure/">secure</a>). Revision 3. </p> <p>Since everyone is posting one of these, I thought I'd post mine. I wrote this many years ago and use it everywhere.</p> Threaded communication with subprocess (Python) 2013-03-13T06:06:16-07:00Jan Müllerhttp://code.activestate.com/recipes/users/4183984/http://code.activestate.com/recipes/578488-threaded-communication-with-subprocess/ <p style="color: grey"> Python recipe 578488 by <a href="/recipes/users/4183984/">Jan Müller</a> (<a href="/recipes/tags/async/">async</a>, <a href="/recipes/tags/ipc/">ipc</a>, <a href="/recipes/tags/messaging/">messaging</a>, <a href="/recipes/tags/multithreading/">multithreading</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/queue/">queue</a>, <a href="/recipes/tags/subprocess/">subprocess</a>). Revision 3. </p> <p>This recipe shows how to domesticate another executable as a service in a subprocess.</p> Hash collision probability / Birthday problem (Python) 2012-12-21T09:32:54-08:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578387-hash-collision-probability-birthday-problem/ <p style="color: grey"> Python recipe 578387 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/birthday/">birthday</a>, <a href="/recipes/tags/collision/">collision</a>, <a href="/recipes/tags/hash/">hash</a>). </p> <p>Calculates the probability that, when making <em>k</em> random selections out of <em>n</em> possibilities, at least two of the selections are the same. See: <a href="http://en.wikipedia.org/wiki/Birthday_problem" rel="nofollow">http://en.wikipedia.org/wiki/Birthday_problem</a></p> <p>What is the probability that (at least) two people in a class of 30 share their birthday?</p> <pre class="prettyprint"><code>&gt;&gt;&gt; collide(30,365) 0.7063162427192688 </code></pre> <p>What is the probability that ORA_HASH generates the same hash when hashing 25000 values?</p> <pre class="prettyprint"><code>&gt;&gt;&gt; collide(25000,int(4.3e9)) 0.07009388771353198 </code></pre> bubblebabble (Python) 2012-11-03T09:28:41-07:00Space Hobohttp://code.activestate.com/recipes/users/4184146/http://code.activestate.com/recipes/578319-bubblebabble/ <p style="color: grey"> Python recipe 578319 by <a href="/recipes/users/4184146/">Space Hobo</a> (<a href="/recipes/tags/babble/">babble</a>, <a href="/recipes/tags/bubble/">bubble</a>, <a href="/recipes/tags/digest/">digest</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/readable/">readable</a>). </p> <p>This module provides a bubblebabble function, which computes a (somewhat more) human readable format for message digests.</p> Flexible datetime parsing (Python) 2012-08-21T07:35:34-07:00Glenn Hutchingshttp://code.activestate.com/recipes/users/4175415/http://code.activestate.com/recipes/578245-flexible-datetime-parsing/ <p style="color: grey"> Python recipe 578245 by <a href="/recipes/users/4175415/">Glenn Hutchings</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parsing/">parsing</a>). </p> <p>The strptime() method of datetime accepts a format string that you have to specify in advance. What if you want to be more flexible in the kinds of date your program accepts? Here's a recipe for a function that tries many different formats until it finds one that works.</p> Recursively defined, Haskell-style infinite lists (Python) 2012-05-04T14:09:14-07:00John Crichtonhttp://code.activestate.com/recipes/users/4181975/http://code.activestate.com/recipes/578119-recursively-defined-haskell-style-infinite-lists/ <p style="color: grey"> Python recipe 578119 by <a href="/recipes/users/4181975/">John Crichton</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/recursive/">recursive</a>). Revision 2. </p> <p>A decorator to simplify the creation of recursively defined, Haskell-style infinite lists -- ie. recursive generators -- inspired by Raymond Hettinger's "Technique for cyclical iteration" [*]. </p> <p>[*] <a href="http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/" rel="nofollow">http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/</a> </p>