Skip to content

Commit

Permalink
Added first method to iterbetter.
Browse files Browse the repository at this point in the history
  • Loading branch information
anandology committed Apr 1, 2014
1 parent 8fa67f4 commit 44e48a4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions web/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,15 @@ class IterBetter:
...
IndexError: already passed 3
It is also possible to get the first value of the iterator or None.
>>> c = iterbetter(iter([3, 4, 5]))
>>> print c.first()
3
>>> c = iterbetter(iter([]))
>>> print c.first()
None
For boolean test, IterBetter peeps at first value in the itertor without effecting the iteration.
>>> c = iterbetter(iter(range(5)))
Expand All @@ -661,6 +670,18 @@ class IterBetter:
def __init__(self, iterator):
self.i, self.c = iterator, 0

def first(self, default=None):
"""Returns the first element of the iterator or None when there are no
elements.
If the optional argument default is specified, that is returned instead
of None when there are no elements.
"""
try:
return iter(self).next()
except StopIteration:
return default

def __iter__(self):
if hasattr(self, "_head"):
yield self._head
Expand Down

0 comments on commit 44e48a4

Please sign in to comment.