Skip to content
This repository has been archived by the owner on Jun 22, 2020. It is now read-only.

Commit

Permalink
lazy_chain: when iterating over a slice, the iterator stopped one ind…
Browse files Browse the repository at this point in the history
…ex too

late.  if using `xfilter()` that could cause needless iteration.
  • Loading branch information
ambv committed Aug 11, 2012
1 parent e46a489 commit 09f6cb6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/lck/django/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ class lazy_chain(object):
Known issues:
1. If slicing or ``xfilter`` is used, reported ``len()`` is computed by
iterating over all iterables so performance is weak.
iterating over all iterables so performance is weak. Note that
``len()`` is used by ``list()`` when you convert your lazy chain to
a list or when iterating over the lazy chain in Django templates.
If this is not expected, you can convert to a list using a workaround
like this::
list(e for e in some_lazy_chain)
2. Indexing on lazy chains uses iteration underneath so performance
is weak. This feature is only available as a last resort. Slicing on the
Expand Down Expand Up @@ -347,9 +353,9 @@ def _gen():
continue
if self.step and index % self.step:
continue
yield self.xform(element)
if self.stop and index >= self.stop:
break
yield self.xform(element)

def __getitem__(self, key):
if isinstance(key, slice):
Expand Down

0 comments on commit 09f6cb6

Please sign in to comment.