Mercurial > python-cmd2
annotate flatten_lines.py @ 319:c58cd7e48db7
begin to switch settable to TextLineList
author | catherine@dellzilla |
---|---|
date | Thu, 11 Feb 2010 13:07:05 -0500 |
parents | f44ad8de0d17 |
children | b9f19255d4b7 |
rev | line source |
---|---|
317 | 1 import doctest |
319
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
2 |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
3 class TextLineList(list): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
4 '''A list that "wants" to consist of separate lines of text. |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
5 Splits multi-line strings and flattens nested lists to |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
6 achieve that. |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
7 Also omits blank lines, strips leading/trailing whitespace. |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
8 |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
9 >>> tll = TextLineList(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']]) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
10 >>> tll |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
11 ['my', 'dog', 'has', 'fleas', 'and', 'ticks'] |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
12 >>> tll.append(['and', ['spiders', 'and']]) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
13 >>> tll |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
14 ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and'] |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
15 >>> tll += 'fish' |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
16 >>> tll |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
17 ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and', 'fish'] |
317 | 18 ''' |
319
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
19 def flattened(self, texts): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
20 result = [] |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
21 if isinstance(texts, basestring): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
22 result.extend(texts.splitlines()) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
23 else: |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
24 for text in texts: |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
25 result.extend(self.flattened(text)) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
26 result = [r.strip() for r in result if r.strip()] |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
27 return result |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
28 def flatten(self): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
29 list.__init__(self, self.flattened(self)) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
30 def __init__(self, values): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
31 list.__init__(self, values) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
32 self.flatten() |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
33 def append(self, value): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
34 list.append(self, value) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
35 self.flatten() |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
36 def extend(self, values): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
37 list.extend(self, values) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
38 self.flatten() |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
39 def __setitem__(self, idx, value): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
40 list.__setitem__(self, idx, value) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
41 self.flatten() |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
42 def __iadd__(self, value): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
43 if isinstance(value, basestring): |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
44 self.append(value) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
45 else: |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
46 list.__iadd__(self, value) |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
47 self.flatten() |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
48 return self |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
49 |
c58cd7e48db7
begin to switch settable to TextLineList
catherine@dellzilla
parents:
318
diff
changeset
|
50 doctest.testmod() |