summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorAmber Brown <hawkowl@atleastfornow.net>2018-05-24 12:54:12 -0500
committerGitHub <noreply@github.com>2018-05-24 12:54:12 -0500
commit2aff6eab6d7ef719a4b7aa2810664d7798245aa7 (patch)
tree85222731e9b1d4d180524a756764b8909f768f02 /synapse/util
parentMerge pull request #3277 from matrix-org/dbkr/remove_from_user_dir (diff)
parentAdd batch_iter to utils (diff)
downloadsynapse-2aff6eab6d7ef719a4b7aa2810664d7798245aa7.tar.xz
Merge pull request #3245 from NotAFile/batch-iter
Add batch_iter to utils
Diffstat (limited to 'synapse/util')
-rw-r--r--synapse/util/__init__.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py
index 814a7bf71b..fc11e26623 100644
--- a/synapse/util/__init__.py
+++ b/synapse/util/__init__.py
@@ -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)), ())