Mercurial > python-cmd2
diff flatten_lines.py @ 317:de23e595bb5c
experiments with line flattening
author | cat@eee |
---|---|
date | Thu, 11 Feb 2010 09:08:22 -0500 |
parents | |
children | f44ad8de0d17 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flatten_lines.py Thu Feb 11 09:08:22 2010 -0500 @@ -0,0 +1,15 @@ +import doctest +def flatten(texts): + ''' + >>> flatten([['cow', 'cat'], '', 'fish', ['bird']]) + ['cow', 'cat', 'fish', 'bird'] + ''' + result = [] + if isinstance(texts, basestring): + result.extend(texts.splitlines()) + else: + for text in texts: + result.extend(flatten(text)) + result = [r.strip() for r in result if r.strip()] + return result +doctest.testmod()