Skip to content

Instantly share code, notes, and snippets.

@Ferada
Last active August 29, 2015 14:21
Show Gist options
  • Save Ferada/94be6137570bc8868b79 to your computer and use it in GitHub Desktop.
Save Ferada/94be6137570bc8868b79 to your computer and use it in GitHub Desktop.
Checked Python izip variant.
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