Skip to content

Instantly share code, notes, and snippets.

@agriffis
Last active August 29, 2015 14:20
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 agriffis/60e1e1727ec8b379486e to your computer and use it in GitHub Desktop.
Save agriffis/60e1e1727ec8b379486e to your computer and use it in GitHub Desktop.
LazyCollectionMixin
class LazyCollectionMixin(object):
__len__ = new_method_proxy(len)
__contains__ = new_method_proxy(operator.contains)
def __iter__(self):
# Don't define this as
# __iter__ = new_method_proxy(iter)
# because that creates a method which doesn't call yield,
# so it executes setup immediately.
if self._wrapped is empty:
self._setup()
for x in self._wrapped:
yield x
def __reduce_ex__(self, version):
if self._wrapped is empty:
self._setup()
return self._wrapped.__reduce_ex__(version)
def __reduce__(self):
if self._wrapped is empty:
self._setup()
return self._wrapped.__reduce__()
class SimpleLazyCollection(LazyCollectionMixin, SimpleLazyObject):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment