class documentation

An iterator object that supports modifying items as they are returned.

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."
Method __init__ No summary
Instance Variable modifier modifier is called with each item in o as it is iterated. The return value of modifier is returned in lieu of the item. Values returned by peek as well as next are affected by modifier. However, sentinel...
Method _fillcache Cache n modified items. If n is 0 or None, 1 item is cached. Each item returned by the iterator is passed through the modify_iter.modifier function before being cached.

Inherited from peek_iter:

Method __iter__ Undocumented
Method __next__ Undocumented
Method has_next Determine if iterator is exhausted.
Method next Get the next item or n items of the iterator.
Method peek Preview the next item or n items of the iterator. The iterator is not advanced when peek is called.
Instance Variable counter Store and increment line number to report correct lines!
Instance Variable sentinel The value used to indicate the iterator is exhausted. If sentinel was not given when the peek_iter was instantiated, then it will be set to a new object instance: object().
Instance Variable _cache Undocumented
Instance Variable _iterable Undocumented
def __init__(self, o: Callable[[], T] | Iterable[T], sentinel: T | None = None, modifier: Callable[[T], T] | None = None): (source)
Parameters
o:Iterable or Callableo is interpreted very differently depending on the presence of sentinel. If sentinel is not given, then o must be a collection object which supports either the iteration protocol or the sequence protocol. If sentinel is given, then o must be a callable object.
sentinel:object, optionalIf given, the iterator will call o with no arguments for each call to its next method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.
modifier:callable, optionalThe function that will be used to modify each item returned by the iterator. modifier should take a single argument and return a single value. Defaults to lambda x: x. If sentinel is not given, modifier must be passed as a keyword argument.

modifier is called with each item in o as it is iterated. The return value of modifier is returned in lieu of the item. Values returned by peek as well as next are affected by modifier. However, sentinel is never passed through modifier; it will always be returned from peek unmodified.

def _fillcache(self, n: int | None): (source)

Cache n modified items. If n is 0 or None, 1 item is cached. Each item returned by the iterator is passed through the modify_iter.modifier function before being cached.