annotate flatten_lines.py @ 318:f44ad8de0d17

more flatten_lines work
author cat@eee
date Thu, 11 Feb 2010 11:25:35 -0500
parents de23e595bb5c
children c58cd7e48db7
rev   line source
317
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
1 import doctest
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
2 def flatten(texts):
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
3 '''
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
4 >>> flatten([['cow', 'cat'], '', 'fish', ['bird']])
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
5 ['cow', 'cat', 'fish', 'bird']
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
6 '''
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
7 result = []
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
8 if isinstance(texts, basestring):
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
9 result.extend(texts.splitlines())
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
10 else:
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
11 for text in texts:
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
12 result.extend(flatten(text))
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
13 result = [r.strip() for r in result if r.strip()]
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
14 return result
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
15 doctest.testmod()
318
f44ad8de0d17 more flatten_lines work
cat@eee
parents: 317
diff changeset
16
f44ad8de0d17 more flatten_lines work
cat@eee
parents: 317
diff changeset
17 class LineList(list):
f44ad8de0d17 more flatten_lines work
cat@eee
parents: 317
diff changeset
18 def __add__(self, newmember):
f44ad8de0d17 more flatten_lines work
cat@eee
parents: 317
diff changeset
19