comparison flatten_lines.py @ 320:b9f19255d4b7

transcript test stuck in infinite loop?
author catherine@dellzilla
date Thu, 11 Feb 2010 14:18:28 -0500
parents c58cd7e48db7
children
comparison
equal deleted inserted replaced
319:c58cd7e48db7 320:b9f19255d4b7
1 import doctest 1 import doctest
2 2
3 class TextLineList(list): 3 class StubbornDict(dict):
4 '''Dictionary that tolerates many input formats.
5 Create it with stubbornDict(arg) factory function.
6
7 >>> d = StubbornDict(large='gross', small='klein')
8 >>> sorted(d.items())
9 [('large', 'gross'), ('small', 'klein')]
10 >>> d.append(['plain', ' plaid'])
11 >>> sorted(d.items())
12 [('large', 'gross'), ('plaid', None), ('plain', None), ('small', 'klein')]
13 >>> d += ' girl Frauelein, Maedchen\\n\\n shoe schuh'
14 >>> sorted(d.items())
15 [('girl', 'Frauelein, Maedchen'), ('large', 'gross'), ('plaid', None), ('plain', None), ('shoe', 'schuh'), ('small', 'klein')]
16 '''
17 def update(self, arg):
18 dict.update(self, StubbornDict.to_dict(arg))
19 append = update
20 def __iadd__(self, arg):
21 self.update(arg)
22 return self
23
24 @classmethod
25 def to_dict(cls, arg):
26 'Generates dictionary from string or list of strings'
27 if hasattr(arg, 'splitlines'):
28 arg = arg.splitlines()
29 if hasattr(arg, '__getslice__'):
30 result = {}
31 for a in arg:
32 a = a.strip()
33 if a:
34 key_val = a.split(None, 1)
35 key = key_val[0]
36 if len(key_val) > 1:
37 val = key_val[1]
38 else:
39 val = None
40 result[key] = val
41 else:
42 result = arg
43 return result
44
45 def stubbornDict(*arg, **kwarg):
46 '''
47 >>> sorted(stubbornDict('cow a bovine\\nhorse an equine').items())
48 [('cow', 'a bovine'), ('horse', 'an equine')]
49 >>> sorted(stubbornDict(['badger', 'porcupine a poky creature']).items())
50 [('badger', None), ('porcupine', 'a poky creature')]
51 >>> sorted(stubbornDict(turtle='has shell', frog='jumpy').items())
52 [('frog', 'jumpy'), ('turtle', 'has shell')]
53 '''
54 result = {}
55 for a in arg:
56 result.update(StubbornDict.to_dict(a))
57 result.update(kwarg)
58 return StubbornDict(result)
59
60
61 class Directory(list):
4 '''A list that "wants" to consist of separate lines of text. 62 '''A list that "wants" to consist of separate lines of text.
5 Splits multi-line strings and flattens nested lists to 63 Splits multi-line strings and flattens nested lists to
6 achieve that. 64 achieve that.
7 Also omits blank lines, strips leading/trailing whitespace. 65 Also omits blank lines, strips leading/trailing whitespace.
8 66
9 >>> tll = TextLineList(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']]) 67 >>> tll = Directory(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']])
10 >>> tll 68 >>> tll
11 ['my', 'dog', 'has', 'fleas', 'and', 'ticks'] 69 ['my', 'dog', 'has', 'fleas', 'and', 'ticks']
12 >>> tll.append(['and', ['spiders', 'and']]) 70 >>> tll.append(['and', ['spiders', 'and']])
13 >>> tll 71 >>> tll
14 ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and'] 72 ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and']