diff 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
line wrap: on
line diff
--- a/flatten_lines.py	Thu Feb 11 13:07:05 2010 -0500
+++ b/flatten_lines.py	Thu Feb 11 14:18:28 2010 -0500
@@ -1,12 +1,70 @@
 import doctest
 
-class TextLineList(list):
+class StubbornDict(dict):
+    '''Dictionary that tolerates many input formats.
+    Create it with stubbornDict(arg) factory function.
+    
+    >>> d = StubbornDict(large='gross', small='klein')
+    >>> sorted(d.items())
+    [('large', 'gross'), ('small', 'klein')]
+    >>> d.append(['plain', '  plaid'])
+    >>> sorted(d.items())
+    [('large', 'gross'), ('plaid', None), ('plain', None), ('small', 'klein')]
+    >>> d += '   girl Frauelein, Maedchen\\n\\n shoe schuh'
+    >>> sorted(d.items())
+    [('girl', 'Frauelein, Maedchen'), ('large', 'gross'), ('plaid', None), ('plain', None), ('shoe', 'schuh'), ('small', 'klein')]
+    '''    
+    def update(self, arg):
+        dict.update(self, StubbornDict.to_dict(arg))
+    append = update
+    def __iadd__(self, arg):
+        self.update(arg)
+        return self
+        
+    @classmethod
+    def to_dict(cls, arg):
+        'Generates dictionary from string or list of strings'
+        if hasattr(arg, 'splitlines'):
+            arg = arg.splitlines()
+        if hasattr(arg, '__getslice__'):
+            result = {}    
+            for a in arg:
+                a = a.strip()
+                if a:
+                    key_val = a.split(None, 1)
+                    key = key_val[0]
+                    if len(key_val) > 1:
+                        val = key_val[1]
+                    else:
+                        val = None
+                    result[key] = val
+        else:
+            result = arg
+        return result
+
+def stubbornDict(*arg, **kwarg):
+    '''
+    >>> sorted(stubbornDict('cow a bovine\\nhorse an equine').items())
+    [('cow', 'a bovine'), ('horse', 'an equine')]
+    >>> sorted(stubbornDict(['badger', 'porcupine a poky creature']).items())
+    [('badger', None), ('porcupine', 'a poky creature')]
+    >>> sorted(stubbornDict(turtle='has shell', frog='jumpy').items())
+    [('frog', 'jumpy'), ('turtle', 'has shell')]
+    '''
+    result = {}
+    for a in arg:
+        result.update(StubbornDict.to_dict(a))
+    result.update(kwarg)                      
+    return StubbornDict(result)
+       
+        
+class Directory(list):
     '''A list that "wants" to consist of separate lines of text.
     Splits multi-line strings and flattens nested lists to 
     achieve that.
     Also omits blank lines, strips leading/trailing whitespace.
     
-    >>> tll = TextLineList(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']])
+    >>> tll = Directory(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']])
     >>> tll
     ['my', 'dog', 'has', 'fleas', 'and', 'ticks']
     >>> tll.append(['and', ['spiders', 'and']])