worker: partition a list (of tasks) into equal-sized chunks
authorBryan O'Sullivan <bryano@fb.com>
Sat, 09 Feb 2013 15:51:32 -0800
changeset 18637 ac4dbceeb14a
parent 18636 dcb27c153a40
child 18638 047110c0e2a8
worker: partition a list (of tasks) into equal-sized chunks
mercurial/worker.py
--- a/mercurial/worker.py	Sat Feb 09 15:51:26 2013 -0800
+++ b/mercurial/worker.py	Sat Feb 09 15:51:32 2013 -0800
@@ -52,3 +52,16 @@
     workers = _numworkers(ui)
     benefit = linear - (_startupcost * workers + linear / workers)
     return benefit >= 0.15
+
+def partition(lst, nslices):
+    '''partition a list into N slices of equal size'''
+    n = len(lst)
+    chunk, slop = n / nslices, n % nslices
+    end = 0
+    for i in xrange(nslices):
+        start = end
+        end = start + chunk
+        if slop:
+            end += 1
+            slop -= 1
+        yield lst[start:end]