#!/usr/bin/env python3
+# © Copyright 2021-2022, Scott Gasch
+
+"""A collection if :class:`Iterator` subclasses that can be composed
+with another iterator and provide extra functionality. e.g.
+
+ + :class:`PeekingIterator`
+ + :class:`PushbackIterator`
+ + :class:`SamplingIterator`
+
+"""
+
import random
from collections.abc import Iterator
from typing import Any, List, Optional
class PeekingIterator(Iterator):
- """An iterator that lets you peek() at the next item on deck.
- Returns None when there is no next item (i.e. when __next__()
- will produce a StopIteration exception).
+ """An iterator that lets you :method:`peek` at the next item on deck.
+ Returns None when there is no next item (i.e. when
+ :method:`__next__` will produce a StopIteration exception).
>>> p = PeekingIterator(iter(range(3)))
>>> p.__next__()
Traceback (most recent call last):
...
StopIteration
+
"""
def __init__(self, source_iter: Iterator):
return self
def __next__(self) -> Any:
- if len(self.pushed_back):
+ if len(self.pushed_back) > 0:
return self.pushed_back.pop()
return self.source_iter.__next__()