Needed the ability to 'pushback' values back onto an iterator, and also was able to add a 'nonzero' test to the iterator at the same time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | class pushback_wrapper(object):
"""
>>> p = pushback_wrapper(iter([1, 2, 3, 37]))
>>> L = []
>>> for x in p:
... L.append(x)
... if x == 2:
... p.pushback(17)
... p.pushback(19)
>>> L
[1, 2, 19, 17, 3, 37]
>>> bool(p)
False
>>> p.pushback(17)
>>> bool(p)
True
>>> p.next()
17
>>> bool(p)
False
>>> p = pushback_wrapper(iter([1, 2]))
>>> bool(p)
True
>>> p.next()
1
>>> p.next()
2
>>> bool(p)
False
>>> p.next()
Traceback (most recent call last):
...
StopIteration
>>> bool(p)
False
>>> p.pushback(17)
>>> bool(p)
True
"""
def __init__(self, it):
self.it = it
self.pushed_back = []
def __iter__(self):
return self
def __nonzero__(self):
if self.pushed_back:
return True
try:
self.pushed_back.insert(0, self.it.next())
except StopIteration:
return False
else:
return True
def next(self):
try:
return self.pushed_back.pop()
except IndexError:
return self.it.next()
def pushback(self, item):
self.pushed_back.append(item)
|
adapted from: http://mail.python.org/pipermail/python-dev/2003-October/038955.html [Python-Dev] generator comprehension syntax, was: accumulator display syntax Alex Martelli aleaxit at yahoo.com Sat Oct 18 05:20:45 EDT 2003
the 'nonzero' test allows you to do quick boolean tests on whether or not the iterator is empty.