Skip to content

Commit

Permalink
Updates examples in iterators module
Browse files Browse the repository at this point in the history
  • Loading branch information
RobRuana committed Feb 19, 2015
1 parent f0ccebb commit 1f6c861
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions pockets/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
class peek_iter(object):
"""An iterator object that supports peeking ahead.
>>> p = peek_iter(["a", "b", "c", "d", "e"])
>>> p.peek()
'a'
>>> p.next()
'a'
>>> p.peek(3)
['b', 'c', 'd']
Args:
o (iterable or callable): `o` is interpreted very differently
depending on the presence of `sentinel`.
Expand Down Expand Up @@ -167,6 +175,20 @@ def peek(self, n=None):
class modify_iter(peek_iter):
"""An iterator object that supports modifying items as they are returned.
>>> a = [" A list ",
... " of strings ",
... " with ",
... " extra ",
... " whitespace. "]
>>> modifier = lambda s: s.strip().replace('with', 'without')
>>> for s in modify_iter(a, modifier=modifier):
... print('"%s"' % s)
"A list"
"of strings"
"without"
"extra"
"whitespace."
Args:
o (iterable or callable): `o` is interpreted very differently
depending on the presence of `sentinel`.
Expand All @@ -190,21 +212,6 @@ class modify_iter(peek_iter):
If `sentinel` is not given, `modifier` must be passed as a keyword
argument.
Example:
>>> a = [" A list ",
... " of strings ",
... " with ",
... " extra ",
... " whitespace. "]
>>> modifier = lambda s: s.strip().replace('with', 'without')
>>> for s in modify_iter(a, modifier=modifier):
... print('"%s"' % s)
"A list"
"of strings"
"without"
"extra"
"whitespace."
Attributes:
modifier (callable): `modifier` is called with each item in `o` as it
is iterated. The return value of `modifier` is returned in lieu of
Expand Down

0 comments on commit 1f6c861

Please sign in to comment.