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_tests.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'morphlib/util_tests.py') diff --git a/morphlib/util_tests.py b/morphlib/util_tests.py index 2ad9e8aa..fbf7f27b 100644 --- a/morphlib/util_tests.py +++ b/morphlib/util_tests.py @@ -110,3 +110,14 @@ class ParseEnvironmentPairsTests(unittest.TestCase): d = { 'a': 1 } morphlib.util.sanitize_environment(d) self.assertTrue(isinstance(d['a'], str)) + +class IterTrickleTests(unittest.TestCase): + + def test_splits(self): + self.assertEqual(list(morphlib.util.iter_trickle("foobarbazqux", 3)), + [["f", "o", "o"], ["b", "a", "r"], + ["b", "a", "z"], ["q", "u", "x"]]) + + def test_truncated_final_sequence(self): + self.assertEqual(list(morphlib.util.iter_trickle("barquux", 3)), + [["b", "a", "r"], ["q", "u", "u"], ["x"]]) -- cgit v1.2.1