annotate flatten_lines.py @ 322:7cd74e42b7f9

transcript test running again
author catherine@dellzilla
date Thu, 11 Feb 2010 14:41:07 -0500
parents b9f19255d4b7
children
rev   line source
317
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
1 import doctest
319
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
2
320
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
3 class StubbornDict(dict):
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
4 '''Dictionary that tolerates many input formats.
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
5 Create it with stubbornDict(arg) factory function.
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
6
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
7 >>> d = StubbornDict(large='gross', small='klein')
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
8 >>> sorted(d.items())
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
9 [('large', 'gross'), ('small', 'klein')]
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
10 >>> d.append(['plain', ' plaid'])
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
11 >>> sorted(d.items())
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
12 [('large', 'gross'), ('plaid', None), ('plain', None), ('small', 'klein')]
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
13 >>> d += ' girl Frauelein, Maedchen\\n\\n shoe schuh'
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
14 >>> sorted(d.items())
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
15 [('girl', 'Frauelein, Maedchen'), ('large', 'gross'), ('plaid', None), ('plain', None), ('shoe', 'schuh'), ('small', 'klein')]
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
16 '''
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
17 def update(self, arg):
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
18 dict.update(self, StubbornDict.to_dict(arg))
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
19 append = update
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
20 def __iadd__(self, arg):
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
21 self.update(arg)
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
22 return self
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
23
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
24 @classmethod
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
25 def to_dict(cls, arg):
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
26 'Generates dictionary from string or list of strings'
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
27 if hasattr(arg, 'splitlines'):
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
28 arg = arg.splitlines()
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
29 if hasattr(arg, '__getslice__'):
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
30 result = {}
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
31 for a in arg:
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
32 a = a.strip()
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
33 if a:
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
34 key_val = a.split(None, 1)
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
35 key = key_val[0]
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
36 if len(key_val) > 1:
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
37 val = key_val[1]
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
38 else:
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
39 val = None
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
40 result[key] = val
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
41 else:
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
42 result = arg
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
43 return result
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
44
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
45 def stubbornDict(*arg, **kwarg):
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
46 '''
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
47 >>> sorted(stubbornDict('cow a bovine\\nhorse an equine').items())
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
48 [('cow', 'a bovine'), ('horse', 'an equine')]
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
49 >>> sorted(stubbornDict(['badger', 'porcupine a poky creature']).items())
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
50 [('badger', None), ('porcupine', 'a poky creature')]
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
51 >>> sorted(stubbornDict(turtle='has shell', frog='jumpy').items())
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
52 [('frog', 'jumpy'), ('turtle', 'has shell')]
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
53 '''
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
54 result = {}
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
55 for a in arg:
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
56 result.update(StubbornDict.to_dict(a))
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
57 result.update(kwarg)
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
58 return StubbornDict(result)
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
59
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
60
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
61 class Directory(list):
319
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
62 '''A list that "wants" to consist of separate lines of text.
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
63 Splits multi-line strings and flattens nested lists to
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
64 achieve that.
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
65 Also omits blank lines, strips leading/trailing whitespace.
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
66
320
b9f19255d4b7 transcript test stuck in infinite loop?
catherine@dellzilla
parents: 319
diff changeset
67 >>> tll = Directory(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']])
319
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
68 >>> tll
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
69 ['my', 'dog', 'has', 'fleas', 'and', 'ticks']
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
70 >>> tll.append(['and', ['spiders', 'and']])
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
71 >>> tll
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
72 ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and']
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
73 >>> tll += 'fish'
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
74 >>> tll
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
75 ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and', 'fish']
317
de23e595bb5c experiments with line flattening
cat@eee
parents:
diff changeset
76 '''
319
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
77 def flattened(self, texts):
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
78 result = []
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
79 if isinstance(texts, basestring):
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
80 result.extend(texts.splitlines())
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
81 else:
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
82 for text in texts:
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
83 result.extend(self.flattened(text))
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
84 result = [r.strip() for r in result if r.strip()]
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
85 return result
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
86 def flatten(self):
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
87 list.__init__(self, self.flattened(self))
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
88 def __init__(self, values):
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
89 list.__init__(self, values)
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
90 self.flatten()
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
91 def append(self, value):
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
92 list.append(self, value)
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
93 self.flatten()
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
94 def extend(self, values):
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
95 list.extend(self, values)
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
96 self.flatten()
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
97 def __setitem__(self, idx, value):
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
98 list.__setitem__(self, idx, value)
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
99 self.flatten()
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
100 def __iadd__(self, value):
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
101 if isinstance(value, basestring):
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
102 self.append(value)
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
103 else:
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
104 list.__iadd__(self, value)
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
105 self.flatten()
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
106 return self
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
107
c58cd7e48db7 begin to switch settable to TextLineList
catherine@dellzilla
parents: 318
diff changeset
108 doctest.testmod()