From ef590d30ca978f57b658f59f584b9785f40ed0a5 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Mon, 11 Nov 2013 11:20:53 +0000 Subject: util: Add helper for splitting up huge iterables This is used to split a large iterable into more manageable chunks. This is important when a consumer can only handle so much input at once e.g. A program can take a large, but finite number of arguments, xargs exists to handle this for shell programs. --- morphlib/util.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'morphlib/util.py') diff --git a/morphlib/util.py b/morphlib/util.py index 04df0633..dd2d05e1 100644 --- a/morphlib/util.py +++ b/morphlib/util.py @@ -13,6 +13,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import itertools import os import re @@ -412,3 +413,12 @@ def get_host_architecture(): # pragma: no cover def sanitize_environment(env): for k in env: env[k] = str(env[k]) + +def iter_trickle(iterable, limit): + '''Split an iterable up into `limit` length chunks.''' + it = iter(iterable) + while True: + buf = list(itertools.islice(it, limit)) + if len(buf) == 0: + break + yield buf -- cgit v1.2.1