Added functions for reading non-comment or empty lines of a file.

This commit is contained in:
Jeremy Fincher 2004-01-11 14:33:38 +00:00
parent 90b4e8bf64
commit 4452ca879e
2 changed files with 24 additions and 0 deletions

View File

@ -511,6 +511,21 @@ class IterableMap(object):
return False 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__': if __name__ == '__main__':
import sys, doctest import sys, doctest
doctest.testmod(sys.modules['__main__']) doctest.testmod(sys.modules['__main__'])

View File

@ -311,6 +311,15 @@ class UtilsTest(unittest.TestCase):
for s in ['lambda: 2', 'import foo', 'foo.bar']: for s in ['lambda: 2', 'import foo', 'foo.bar']:
self.assertRaises(ValueError, utils.safeEval, s) 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'])