Popular recipes tagged "lazy"http://code.activestate.com/recipes/tags/lazy/popular/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> 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> 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> Super lazy load object (Python) 2010-03-16T05:02:12-07:00Russellhttp://code.activestate.com/recipes/users/4173357/http://code.activestate.com/recipes/577117-super-lazy-load-object/ <p style="color: grey"> Python recipe 577117 by <a href="/recipes/users/4173357/">Russell</a> (<a href="/recipes/tags/lazy/">lazy</a>). </p> <p>A really light implementation of lazy load technique, yet powerful and conveniet. </p> <p>Simply call this:</p> <pre class="prettyprint"><code>var1 = superlazy('key', default_value) </code></pre> <p>Your var1 will be loaded in load_setting(key) when accessed first time.</p> <p>That's it. No subclassing is needed, no declaration is needed. Value type is auto detected and handled gracefully. str, int, list, dict can all be lazily loaded from anywhere you want now.</p> lazy property (Python) 2009-04-26T18:10:55-07:00Sridhar Ratnakumarhttp://code.activestate.com/recipes/users/4169511/http://code.activestate.com/recipes/576720-lazy-property/ <p style="color: grey"> Python recipe 576720 by <a href="/recipes/users/4169511/">Sridhar Ratnakumar</a> (<a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/lazy_evaluation/">lazy_evaluation</a>). Revision 6. </p> <p>Python does not have lazy evaluation syntax features built-in, but fortunately decorators can be used with new-style classes to emulate such a feature. There are cases where one wants <code>foo.property</code> to return the actual property whose calculation takes significant amount of time.</p> <p>This recipe adapts the existing <code>property</code> to provide a <code>lazypropery</code> decorator that does this.</p> <p>See the first comment below for an example usage.</p> <p>Also see: <a href="http://en.wikipedia.org/wiki/Lazy_initialization">lazy initialization</a></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>