Popular recipes tagged "iterator" but not "cyclic_iterator"http://code.activestate.com/recipes/tags/iterator-cyclic_iterator/2015-01-29T17:12:10-08:00ActiveState Code RecipesOdometer iterator (Python) 2014-09-27T22:35:47-07:00Tomas Nordinhttp://code.activestate.com/recipes/users/4189558/http://code.activestate.com/recipes/578944-odometer-iterator/ <p style="color: grey"> Python recipe 578944 by <a href="/recipes/users/4189558/">Tomas Nordin</a> (<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/odometer/">odometer</a>, <a href="/recipes/tags/radix/">radix</a>). Revision 2. </p> <p>A suggestion of an odometer implementation. Each "roll" in the odometer is defined by the caller. It is a list holding any objects, only the length of the list is important.</p> <p>Can be good to have if something need to be tested in all possible combinations.</p> Forward iterator (Python) 2015-01-29T17:12:10-08:00Andrea Corbellinihttp://code.activestate.com/recipes/users/4186880/http://code.activestate.com/recipes/578560-forward-iterator/ <p style="color: grey"> Python recipe 578560 by <a href="/recipes/users/4186880/">Andrea Corbellini</a> (<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>). Revision 2. </p> <p>A proxy for iterators that lets you <em>read ahead</em> items without consuming the iterator.</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> An analogue of enumerate for nested lists. (Python) 2012-12-11T16:59:14-08:00John Crichtonhttp://code.activestate.com/recipes/users/4181975/http://code.activestate.com/recipes/578376-an-analogue-of-enumerate-for-nested-lists/ <p style="color: grey"> Python recipe 578376 by <a href="/recipes/users/4181975/">John Crichton</a> (<a href="/recipes/tags/enumerate/">enumerate</a>, <a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/list/">list</a>). Revision 6. </p> <p>A generator function which enables traversal and modification of deeply nested lists. Together with the supplied helper functions it could be useful when working with data stored in deeply nested lists, particularly when the level of nesting precludes a recursive approach.</p> Flattening an arbitrarily deep list (or any iterator) (Python) 2012-04-03T17:13:35-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578092-flattening-an-arbitrarily-deep-list-or-any-iterato/ <p style="color: grey"> Python recipe 578092 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tuple/">tuple</a>). Revision 6. </p> <p>What if you had a list like this: [1, -10, [1,2,[3,4]], xrange(200)], and you just wanted to go through each element in order (wanted it to return a simple list of [1,-10,1,2,3,4,1,2,3,4...199])</p> <p>I've seen a couple of attempts to flatten arbitrarily deep lists. Many of them involve recursion, like this one: <a href="http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html" rel="nofollow">http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html</a></p> <p>Recursion is generally considered non-pythonic (at least to my knowledge), so I have used one which just involves simple iterators instead. Also, recursion will fail if the list is too deep (so it wouldn't really be arbitrary, would it?).</p> lazy ordered unique elements from an iterator (Python) 2011-06-23T16:36:38-07:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/577768-lazy-ordered-unique-elements-from-an-iterator/ <p style="color: grey"> Python recipe 577768 by <a href="/recipes/users/912777/">Andrew Dalke</a> (<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/unique/">unique</a>). Revision 2. </p> <p>This implements a "unique" filter. Its input is an iterator of hashable items. It returns an iterator containing only the unique items of the input, in input order. That is, list(unique("cabbage")) produces ["c", "a", "b", "g"]. The implementation is lazy. The function supports the "key" parameter, which provides an alternate form of comparison.</p> <p>(Note: a better version of this is available from the itertools documentation as unique_everseen )</p> Fast flatten() with depth control and oversight over which subtrees to expand (Python) 2010-11-26T11:10:01-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577470-fast-flatten-with-depth-control-and-oversight-over/ <p style="color: grey"> Python recipe 577470 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/tuple/">tuple</a>). </p> <p>Extremely fast, non-recursive, depth limited flatten with powerful control over which subtrees are to be expanded. If this is what you need then look no further.</p> peek ahead an iterator (Python) 2010-08-17T13:41:41-07:00Quintijn Hoogenboomhttp://code.activestate.com/recipes/users/4171692/http://code.activestate.com/recipes/577361-peek-ahead-an-iterator/ <p style="color: grey"> Python recipe 577361 by <a href="/recipes/users/4171692/">Quintijn Hoogenboom</a> (<a href="/recipes/tags/iter/">iter</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/peek/">peek</a>, <a href="/recipes/tags/peeking/">peeking</a>). </p> <p>a merge of recipes 16.7 and 19.18 of the python cookbook (2nd edition).</p> <p>you can peek ahead more steps if needed, or just use the preview variable for the next item of the list.</p> PHP 'Struct' port (PHP) 2010-03-28T01:22:31-07:00Jeff Griffithshttp://code.activestate.com/recipes/users/835605/http://code.activestate.com/recipes/577160-php-struct-port/ <p style="color: grey"> PHP recipe 577160 by <a href="/recipes/users/835605/">Jeff Griffiths</a> (<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>In Ruby, the <a href="http://ruby-doc.org/core/classes/Struct.html">Struct class</a> is a convenient way to create a hash-like object on the fly and use it for your nefarious purposes. PHP 5+ can be convinced to do this type of things as well, it just doesn't have it out of the box. Here is a simple class that implements iterator and allows you to populate the internal data structure similar to how Ruby's Struct works. Syntactic sugar? Probably.</p> <p>Note: I haven't bothered to implement the Ruby Struct API per se, Instead I just got something similar by implementing the Iterator interface and keeping things very PHP-like.</p> Windowing an iterable with itertools (Python) 2010-04-15T18:45:41-07:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577196-windowing-an-iterable-with-itertools/ <p style="color: grey"> Python recipe 577196 by <a href="/recipes/users/4172918/">Daniel Cohn</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/loop/">loop</a>, <a href="/recipes/tags/peeking/">peeking</a>, <a href="/recipes/tags/window/">window</a>). </p> <p>Oftentimes a programmer needs to peek into an iterator without advancing it, a task for which many good solutions already exist. But what if the intrepid coder needs a fast and pythonic way to 'window' the data? This recipe demonstrates how to wrap any iterable with a class that adds two methods, prev and peek.</p> iter_except -- a useful variant of __builtin__.iter() (Python) 2010-03-27T02:04:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577155-iter_except-a-useful-variant-of-__builtin__iter/ <p style="color: grey"> Python recipe 577155 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>). </p> <p>Variant of iter(func, sentinel) that looks for an exception rather than for a sentinel value. Good for making iterators from of APIs that advance over a data and return an exception when they are done.</p> Concurrent buffer for generators (Python) 2010-05-05T22:47:33-07:00Javier Ruerehttp://code.activestate.com/recipes/users/4172765/http://code.activestate.com/recipes/576999-concurrent-buffer-for-generators/ <p style="color: grey"> Python recipe 576999 by <a href="/recipes/users/4172765/">Javier Ruere</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/threading/">threading</a>). Revision 2. </p> <p>A buffer that will partially consume an iterator in the background.</p> <p>Very useful for reading files and merging the data using the excellent <a href="http://code.activestate.com/recipes/491285/" rel="nofollow">http://code.activestate.com/recipes/491285/</a></p> Caching iterable wrapper (Python) 2009-11-06T11:38:43-08:00Ulrik Sverdruphttp://code.activestate.com/recipes/users/4172166/http://code.activestate.com/recipes/576941-caching-iterable-wrapper/ <p style="color: grey"> Python recipe 576941 by <a href="/recipes/users/4172166/">Ulrik Sverdrup</a> (<a href="/recipes/tags/cached/">cached</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterator/">iterator</a>). Revision 8. </p> <p>This is a very simple wrapper of an iterator or iterable, such that the iterator can be iterated streamingly without generating all elements or any at all, but the object can still be iterated from the beginning as many times as wanted. In effect, it is a streamingly loaded list.</p> Partition an iterable into n lists (Python) 2009-05-30T16:35:27-07:00Ian Eloffhttp://code.activestate.com/recipes/users/2227021/http://code.activestate.com/recipes/576785-partition-an-iterable-into-n-lists/ <p style="color: grey"> Python recipe 576785 by <a href="/recipes/users/2227021/">Ian Eloff</a> (<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/partition/">partition</a>). </p> <p>This could also be easily modified to return n iterators, but was outside of my needs. Handy for splitting up the workload for use with multiple threads/processes.</p>