Skip to content

Instantly share code, notes, and snippets.

@mk270
Last active March 16, 2021 14:27
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 mk270/4320685 to your computer and use it in GitHub Desktop.
Save mk270/4320685 to your computer and use it in GitHub Desktop.
batched generator for Python
import itertools
def batch_generator(size, g):
buffer = []
try:
while True:
for i in range(0, size):
buffer.append(g.next())
yield buffer
buffer = []
except StopIteration:
if 0 == len(buffer):
raise
else:
yield buffer
@mk270
Copy link
Author

mk270 commented Mar 2, 2021

@danbst thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment