mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
Added functions for reading non-comment or empty lines of a file.
This commit is contained in:
parent
90b4e8bf64
commit
4452ca879e
15
src/utils.py
15
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__'])
|
||||
|
@ -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'])
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user