Last active
August 29, 2015 14:20
-
-
Save agriffis/60e1e1727ec8b379486e to your computer and use it in GitHub Desktop.
LazyCollectionMixin
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
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