Package web :: Package web :: Module utils :: Class IterBetter
[hide private]
[frames] | no frames]

Class IterBetter

source code

Returns an object that can be used as an iterator but can also be used via __getitem__ (although it cannot go backwards -- that is, you cannot request `iterbetter[0]` after requesting `iterbetter[1]`).

>>> import itertools
>>> c = iterbetter(itertools.count())
>>> c[1]
1
>>> c[5]
5
>>> c[3]
Traceback (most recent call last):
    ...
IndexError: already passed 3

For boolean test, IterBetter peeps at first value in the itertor without effecting the iteration.

>>> c = iterbetter(iter(range(5)))
>>> bool(c)
True
>>> list(c)
[0, 1, 2, 3, 4]
>>> c = iterbetter(iter([]))
>>> bool(c)
False
>>> list(c)
[]
Instance Methods [hide private]
 
__init__(self, iterator) source code
 
__iter__(self) source code
 
__getitem__(self, i) source code
 
__nonzero__(self) source code