Skip to content

Instantly share code, notes, and snippets.

@ianjosephwilson
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 ianjosephwilson/3c63309781d940233669 to your computer and use it in GitHub Desktop.
Save ianjosephwilson/3c63309781d940233669 to your computer and use it in GitHub Desktop.
class BatchIterator(object):
"""
Return batches of items of batch size or less from the given source.
Each batch is another iterator.
"""
def __init__(self, source, batch_size=900):
self.source_iter = iter(source)
self.batch_size = batch_size
self.stop_iteration = False
def __iter__(self):
return self
def __next__(self):
if self.stop_iteration:
raise StopIteration
def batch():
for item in self.source_iter:
self.index += 1
yield item
if self.index % self.batch_size == 0 and self.index != 0:
break
else:
self.stop_iteration = True
return batch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment