Popular recipes tagged "generators"http://code.activestate.com/recipes/tags/generators/2017-05-12T10:40:58-07:00ActiveState Code Recipesgroupby() For Unsorted Input (Python) 2017-05-12T10:40:58-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580800-groupby-for-unsorted-input/ <p style="color: grey"> Python recipe 580800 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/grouping/">grouping</a>, <a href="/recipes/tags/lazy/">lazy</a>). </p> <p>We all know the <code>groupby()</code> which is available in the <code>itertools</code> standard module. This one yields groups of consecutive elements in the input which are meant to be together in one group. For non-consecutive elements this will yield more than one group for the same key.</p> <p>So effectively, <code>groupby()</code> only reformats a flat list into bunches of elements from that list without reordering anything. In practice this means that for input sorted by key this works perfect, but for unsorted input it might yield several groups for the same key (with groups for other keys in between). Typically needed, though, is a grouping with reordering if necessary.</p> <p>I implemented a likewise lazy function (yielding generators) which also accepts ungrouped input.</p> Pluggable Python generators (Python) 2016-03-18T19:11:48-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580628-pluggable-python-generators/ <p style="color: grey"> Python recipe 580628 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a simple recipe to show that Python generators are pluggable, i.e., they can be passed as arguments into functions, and then used inside those functions.</p> where() function for generator expressions (Python 3) (Python) 2014-10-05T04:10:21-07:00Alan Cristhian Ruizhttp://code.activestate.com/recipes/users/4186199/http://code.activestate.com/recipes/578946-where-function-for-generator-expressions-python-3/ <p style="color: grey"> Python recipe 578946 by <a href="/recipes/users/4186199/">Alan Cristhian Ruiz</a> (<a href="/recipes/tags/generators/">generators</a>). Revision 3. </p> <p>Function that work like an "where statement" for generator expression. The code below</p> <pre class="prettyprint"><code>x, y, z = 1, 2, 3 ((x, y, z) for _ in range(5)) </code></pre> <p>Is equivalent to:</p> <pre class="prettyprint"><code>((x, y, z) for _ in range(5)) &lt; where(x=1, y=2, z=3) </code></pre> Generator with lookahead (Python) 2013-09-24T10:44:56-07:00Rutger Saalminkhttp://code.activestate.com/recipes/users/4187940/http://code.activestate.com/recipes/578671-generator-with-lookahead/ <p style="color: grey"> Python recipe 578671 by <a href="/recipes/users/4187940/">Rutger Saalmink</a> (<a href="/recipes/tags/background/">background</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/lookahead/">lookahead</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Python generators are a great way of reducing memory usage due to the lazy (on demand) generation of values. However, when the process-time of generating such a value is relatively high, we can improve performance even more by obtaining the next n values of the generator in a separate thread in the background. Hence, the BackgroundGenerator.</p> Indexable Generator (Python) 2012-01-03T06:10:18-08:00Peter Donishttp://code.activestate.com/recipes/users/4180313/http://code.activestate.com/recipes/578000-indexable-generator/ <p style="color: grey"> Python recipe 578000 by <a href="/recipes/users/4180313/">Peter Donis</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/generators/">generators</a>). Revision 4. </p> <p>A decorator for generators that makes them indexable like a sequence.</p> Memoize Generator (Python) 2012-01-02T05:53:19-08:00Peter Donishttp://code.activestate.com/recipes/users/4180313/http://code.activestate.com/recipes/577992-memoize-generator/ <p style="color: grey"> Python recipe 577992 by <a href="/recipes/users/4180313/">Peter Donis</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/memoize/">memoize</a>). Revision 2. </p> <p>A wrapper class for generators that "memoizes" them, so that even if the generator is realized multiple times, each term only gets computed once (after that the result is simply returned from a cache).</p> Permutation and combination using recursive generator (Python) 2011-10-04T05:44:19-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577890-permutation-and-combination-using-recursive-genera/ <p style="color: grey"> Python recipe 577890 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/combinatorics/">combinatorics</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/permutation/">permutation</a>, <a href="/recipes/tags/recursion/">recursion</a>). Revision 2. </p> <p>This recipes demonstrates how to use recursive generator to implement permutation and combination.</p> Pure Python implementation of PEP 380 (yield from) (Python) 2009-04-25T09:03:42-07:00profjimhttp://code.activestate.com/recipes/users/4169991/http://code.activestate.com/recipes/576727-pure-python-implementation-of-pep-380-yield-from/ <p style="color: grey"> Python recipe 576727 by <a href="/recipes/users/4169991/">profjim</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generators/">generators</a>). Revision 7. </p> <p>Decorator-based implementation of PEP 380 (yield from). This is the simple version (no special handling of nested "yield _from"s).</p> Pure Python implementation of PEP 380 (yield from) -- Optimized (Python) 2009-04-29T09:11:31-07:00profjimhttp://code.activestate.com/recipes/users/4169991/http://code.activestate.com/recipes/576728-pure-python-implementation-of-pep-380-yield-from-o/ <p style="color: grey"> Python recipe 576728 by <a href="/recipes/users/4169991/">profjim</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generators/">generators</a>). Revision 6. </p> <p>Decorator-based implementation of PEP 380 (yield from). This is the optimized version (special handling of nested "yield _from"s).</p> Interleaving file lines using iterators (Python) 2009-02-23T04:07:37-08:00David Mosshttp://code.activestate.com/recipes/users/4124829/http://code.activestate.com/recipes/576665-interleaving-file-lines-using-iterators/ <p style="color: grey"> Python recipe 576665 by <a href="/recipes/users/4124829/">David Moss</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/manipulation/">manipulation</a>). </p> <p>Accepts one of more files and/or globs and interleaves the lines from each writing the result to stdout.</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> Lazy Lists for python 2.5 (Python) 2008-08-18T19:59:03-07:00Michael Pusthttp://code.activestate.com/recipes/users/4166509/http://code.activestate.com/recipes/576429-lazy-lists-for-python-25/ <p style="color: grey"> Python recipe 576429 by <a href="/recipes/users/4166509/">Michael Pust</a> (<a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/memoization/">memoization</a>). </p> <p>Dan Spitz submitted a recipe ( <a href="http://code.activestate.com/recipes/576410/">576410</a> ) for recursively defined lazy lists backed by a generator. Only catch was that it was written for python 3k. But there is nothing intrinsic in 3k that prevents you from having the same functionality in python 2.5, so I am supplying the backport here.</p> Lazy Lists (Python) 2008-08-08T10:23:52-07:00Dan Spitzhttp://code.activestate.com/recipes/users/4166237/http://code.activestate.com/recipes/576410-lazy-lists/ <p style="color: grey"> Python recipe 576410 by <a href="/recipes/users/4166237/">Dan Spitz</a> (<a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/memoization/">memoization</a>). Revision 6. </p> <p>This is a (python 3.0) recipe for LazyLists, or ordered sequences whose contents are generated lazily by an iterator.</p>