From 6a9b977826a6a45b28a9d9226c5d9b56ce8d72a6 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann Date: Thu, 4 Oct 2012 19:58:45 +0200 Subject: [PATCH 1/1] utils.FilterEmptyLinesAndComments: Return list We don't use generators often and lists are easier to re-use. Signed-off-by: Michael Hanselmann Reviewed-by: Bernardo Dal Seno --- lib/utils/text.py | 11 ++++------- test/ganeti.utils.text_unittest.py | 14 +++++--------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/utils/text.py b/lib/utils/text.py index 3da8f83..84de0bb 100644 --- a/lib/utils/text.py +++ b/lib/utils/text.py @@ -598,12 +598,9 @@ def FilterEmptyLinesAndComments(text): @type text: string @param text: Input string - @rtype: generator + @rtype: list """ - for line in text.splitlines(): - line = line.strip() - - # Ignore empty lines and comments - if line and not line.startswith("#"): - yield line + return [line for line in map(lambda s: s.strip(), text.splitlines()) + # Ignore empty lines and comments + if line and not line.startswith("#")] diff --git a/test/ganeti.utils.text_unittest.py b/test/ganeti.utils.text_unittest.py index e118182..16dd6ac 100755 --- a/test/ganeti.utils.text_unittest.py +++ b/test/ganeti.utils.text_unittest.py @@ -594,15 +594,11 @@ class TestTruncate(unittest.TestCase): class TestFilterEmptyLinesAndComments(unittest.TestCase): - @staticmethod - def _Test(text): - return list(utils.FilterEmptyLinesAndComments(text)) - def testEmpty(self): - self.assertEqual(self._Test(""), []) - self.assertEqual(self._Test("\n"), []) - self.assertEqual(self._Test("\n" * 100), []) - self.assertEqual(self._Test("\n \n\t \n"), []) + self.assertEqual(utils.FilterEmptyLinesAndComments(""), []) + self.assertEqual(utils.FilterEmptyLinesAndComments("\n"), []) + self.assertEqual(utils.FilterEmptyLinesAndComments("\n" * 100), []) + self.assertEqual(utils.FilterEmptyLinesAndComments("\n \n\t \n"), []) def test(self): text = """ @@ -618,7 +614,7 @@ class TestFilterEmptyLinesAndComments(unittest.TestCase): # multiple places Hello World! """ - self.assertEqual(self._Test(text), [ + self.assertEqual(utils.FilterEmptyLinesAndComments(text), [ "This", "is", "a", -- 1.7.10.4