Helper to easily implement an iterator. The constructor gets a container which must implement __len__ and __getitem__
| 202 | |
| 203 | |
| 204 | class GenericIterator(Generic[T]): |
| 205 | """Helper to easily implement an iterator. |
| 206 | |
| 207 | The constructor gets a container which must implement __len__ and |
| 208 | __getitem__ |
| 209 | """ |
| 210 | |
| 211 | def __init__(self, container: SequenceProtocol[T]) -> None: |
| 212 | self.container = container |
| 213 | self.length = len(container) |
| 214 | self.idx = 0 |
| 215 | |
| 216 | def __iter__(self) -> Iterator[T]: |
| 217 | return self |
| 218 | |
| 219 | def __next__(self) -> T: |
| 220 | idx = self.idx |
| 221 | if idx >= self.length: |
| 222 | raise StopIteration |
| 223 | |
| 224 | self.idx += 1 |
| 225 | return self.container[idx] |