Created
February 19, 2015 20:37
-
-
Save clayg/641503fa10a90c368458 to your computer and use it in GitHub Desktop.
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 | |
import sys | |
def app_iter(): | |
for i in range(10): | |
yield i | |
class ProxiedIterable(object): | |
def __init__(self, wrapped): | |
self.wrapped = wrapped | |
def __enter__(self): | |
self.iterable = iter(self.wrapped) | |
return self | |
def __exit__(self, *args, **kwargs): | |
print "le close" | |
self.iterable.close() | |
def __iter__(self): | |
return self | |
def next(self): | |
return next(self.iterable) | |
def __next__(self): | |
return self.next() | |
def wrapped_response(response): | |
with ProxiedIterable(response) as resp: | |
for part in resp: | |
if part % 2: | |
yield part | |
response = app_iter() | |
w = wrapped_response(response) | |
if '--fail' in sys.argv: | |
for i in range(3): | |
print next(w) | |
w.close() | |
else: | |
for i in w: | |
print i | |
print 'done.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment