Skip to content

Instantly share code, notes, and snippets.

@theelous3
Created April 18, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theelous3/9c19f7c76086c4b971f911e803873249 to your computer and use it in GitHub Desktop.
Save theelous3/9c19f7c76086c4b971f911e803873249 to your computer and use it in GitHub Desktop.
class LastLoopCacherNoFinal:
pass
class LastLoopCacher:
"""
An iterable context manager that stores the last value it loops as
an attribute, `final`.
Final falls out of scope upon exiting the context manager.
For use in cases where many objects are looped but not all of their attrs
are of iterest. Normally this requires you to contiuously repeatedly store
the same data over and over again, but with this we can catch and cache the
last one.
"""
def __init__(self, xs):
self.xs = xs
self._final = LastLoopCacherNoFinal
def __iter__(self):
ix = iter(self.xs)
while True:
try:
x = next(ix)
yield x
except StopIteration:
try:
self._final = x
raise StopIteration
except UnboundLocalError:
raise RuntimeError("Cannot rely on cache of empty iterable.")
def __enter__(self):
return self
def __exit__(self, type_, value, traceback):
self.reset()
@property
def final(self):
if self._final is LastLoopCacherNoFinal:
raise RuntimeError("Cannot access final loop out of scope.")
return self._final
def reset(self):
self._final = LastLoopCacherNoFinal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment