Update docs.
[python_utils.git] / iter_utils.py
index b1a137c33a7c83c0c221492511240b803a1684ad..69f58226aada7b718f6e938e2d635a74fea5ba53 100644 (file)
@@ -1,5 +1,16 @@
 #!/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
@@ -7,8 +18,8 @@ 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).
+    Returns None when there is no next item (i.e. when
+    __next__() will produce a StopIteration exception).
 
     >>> p = PeekingIterator(iter(range(3)))
     >>> p.__next__()
@@ -27,6 +38,7 @@ class PeekingIterator(Iterator):
     Traceback (most recent call last):
       ...
     StopIteration
+
     """
 
     def __init__(self, source_iter: Iterator):
@@ -88,7 +100,7 @@ class PushbackIterator(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__()
 
@@ -101,11 +113,13 @@ class SamplingIterator(Iterator):
     collects a random sample (of size sample_size) of the stream that can
     be queried at any time.
 
-    Note that until sample_size elements have been seen the sample will
-    be less than sample_size elements in length.
+    .. note::
+        Until sample_size elements have been seen the sample will be
+        less than sample_size elements in length.
 
-    Note that if sample_size is > len(source_iter) then it will produce
-    a copy of source_iter.
+    .. note::
+        If sample_size is > len(source_iter) then it will produce a
+        copy of source_iter.
 
     >>> import collections
     >>> import random