From 4452ca879e18163efde8e07e2e506bb3ce4131dc Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Sun, 11 Jan 2004 14:33:38 +0000 Subject: [PATCH] Added functions for reading non-comment or empty lines of a file. --- src/utils.py | 15 +++++++++++++++ test/test_utils.py | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/src/utils.py b/src/utils.py index 68e8251c9..15c5f4d78 100755 --- a/src/utils.py +++ b/src/utils.py @@ -511,6 +511,21 @@ class IterableMap(object): return False +def nonCommentLines(fd): + for line in fd: + if not line.startswith('#'): + yield line + +def nonEmptyLines(fd): +## for line in fd: +## if line.strip(): +## yield line + return ifilter(str.strip, fd) + +def nonCommentNonEmptyLines(fd): + return nonEmptyLines(nonCommentLines(fd)) + + if __name__ == '__main__': import sys, doctest doctest.testmod(sys.modules['__main__']) diff --git a/test/test_utils.py b/test/test_utils.py index 508615175..aa7afefad 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -311,6 +311,15 @@ class UtilsTest(unittest.TestCase): for s in ['lambda: 2', 'import foo', 'foo.bar']: self.assertRaises(ValueError, utils.safeEval, s) + def testLines(self): + L = ['foo', 'bar', '#baz', ' ', 'biff'] + self.assertEqual(list(utils.nonEmptyLines(L)), + ['foo', 'bar', '#baz', 'biff']) + self.assertEqual(list(utils.nonCommentLines(L)), + ['foo', 'bar', ' ', 'biff']) + self.assertEqual(list(utils.nonCommentNonEmptyLines(L)), + ['foo', 'bar', 'biff']) +