Skip to content
Snippets Groups Projects
Unverified Commit 2aff6eab authored by Amber Brown's avatar Amber Brown Committed by GitHub
Browse files

Merge pull request #3245 from NotAFile/batch-iter

Add batch_iter to utils
parents ecc4b88b 45b55e23
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,8 @@ from twisted.internet import defer, reactor, task
import time
import logging
from itertools import islice
logger = logging.getLogger(__name__)
......@@ -79,3 +81,19 @@ class Clock(object):
except Exception:
if not ignore_errs:
raise
def batch_iter(iterable, size):
"""batch an iterable up into tuples with a maximum size
Args:
iterable (iterable): the iterable to slice
size (int): the maximum batch size
Returns:
an iterator over the chunks
"""
# make sure we can deal with iterables like lists too
sourceiter = iter(iterable)
# call islice until it returns an empty tuple
return iter(lambda: tuple(islice(sourceiter, size)), ())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment