launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #02545
[Merge] lp:~allenap/launchpad/better-caching-iterator into lp:launchpad
Gavin Panella has proposed merging lp:~allenap/launchpad/better-caching-iterator into lp:launchpad.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~allenap/launchpad/better-caching-iterator/+merge/48927
Use itertools.tee() to simplify and almost certainly speed up CachingIterator. This is a drive-by improvement.
--
https://code.launchpad.net/~allenap/launchpad/better-caching-iterator/+merge/48927
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~allenap/launchpad/better-caching-iterator into lp:launchpad.
=== modified file 'lib/lp/services/utils.py'
--- lib/lp/services/utils.py 2011-01-26 22:07:08 +0000
+++ lib/lp/services/utils.py 2011-02-08 15:03:59 +0000
@@ -19,7 +19,7 @@
]
from textwrap import dedent
-import itertools
+from itertools import tee
from lazr.enum import BaseItem
from twisted.python.util import mergeFunctionMetadata
@@ -123,28 +123,12 @@
def __init__(self, iterator):
self.iterator = iterator
- self.data = []
def __iter__(self):
- index = itertools.count()
- while True:
- pos = index.next()
- try:
- yield self.data[pos]
- except IndexError:
- # Defer to the iterator.
- pass
- else:
- continue
- if self.iterator is None:
- break
- try:
- item = self.iterator.next()
- except StopIteration:
- self.iterator = None
- break
- self.data.append(item)
- yield item
+ # Teeing an iterator previously returned by tee won't cause heat
+ # death. See tee_copy in itertoolsmodule.c in the Python source.
+ self.iterator, iterator = tee(self.iterator)
+ return iterator
def decorate_with(context_factory, *args, **kwargs):