Last active
March 16, 2021 14:27
-
-
Save mk270/4320685 to your computer and use it in GitHub Desktop.
batched generator for Python
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@danbst thank you!