Last active
August 29, 2015 14:21
-
-
Save Ferada/94be6137570bc8868b79 to your computer and use it in GitHub Desktop.
Checked Python izip variant.
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 izip_checked(object): | |
def __init__(self, *args): | |
self._iterators = map(iter, args) | |
def __iter__(self): | |
if self._iterators: | |
return self | |
return iter([]) | |
def next(self): | |
results = [] | |
stop = False | |
for iterator in self._iterators: | |
try: | |
results.append(iterator.next()) | |
if stop: | |
raise Exception("Later iterators succeeded, returned {}.".format(results)) | |
except StopIteration: | |
if results: | |
raise Exception("Earlier iterators succeeded, returned {}.".format(results)) | |
stop = True | |
if stop: | |
raise StopIteration() | |
return tuple(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment