utils.FilterEmptyLinesAndComments: Return list
authorMichael Hanselmann <hansmi@google.com>
Thu, 4 Oct 2012 17:58:45 +0000 (19:58 +0200)
committerMichael Hanselmann <hansmi@google.com>
Fri, 5 Oct 2012 01:52:03 +0000 (03:52 +0200)
We don't use generators often and lists are easier to re-use.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Bernardo Dal Seno <bdalseno@google.com>

lib/utils/text.py
test/ganeti.utils.text_unittest.py

index 3da8f83..84de0bb 100644 (file)
@@ -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("#")]
index e118182..16dd6ac 100755 (executable)
@@ -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",