Created
July 11, 2023 21:45
-
-
Save marklap/082e5f81a2836447a61e833f963f17f3 to your computer and use it in GitHub Desktop.
An implementation of a generator that detects when the last item has been consumed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
################################################################################ | |
# The MIT License (MIT) | |
# | |
# Copyright (c) 2023 Mark LaPerriere | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
################################################################################ | |
class PeekableGenerator: | |
""" PeekableGenerator is a wrapper for generators to offer the convenience of knowning when there are no more | |
elements left in a generator. In practice it simply looks ahead to the next value while presenting the | |
previous. | |
""" | |
# _THE_END is a sentinel value to distinguish between a None returned from calling next() and a StopIteration | |
_THE_END = object() | |
def __init__(self, generator: Generator): | |
self._generator = generator | |
self._next_item: Any = self._peek() | |
@property | |
def is_last_item(self) -> bool: | |
""" is_last_item communicates that the current value will be the last to be returned by the generator. | |
""" | |
return self._next_item is PeekableGenerator._THE_END | |
def _peek(self) -> Any: | |
""" _peek calls `next()` on the generator and if it hits a StopIteration, it prepares this wrapper to do the | |
same on the caller's next call to `next()`. | |
""" | |
try: | |
return next(self._generator) | |
except StopIteration: | |
return PeekableGenerator._THE_END | |
def __iter__(self): | |
return self | |
def __next__(self) -> Any: | |
""" _next raises a StopIteration if the last value was THE LAST value otherwise it stores the next value and | |
returns the previous value. | |
""" | |
if self.is_last_item: | |
raise StopIteration() | |
ret = self._next_item | |
self._next_item = self._peek() | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment