Mercurial > python-cmd2
changeset 320:b9f19255d4b7
transcript test stuck in infinite loop?
author | catherine@dellzilla |
---|---|
date | Thu, 11 Feb 2010 14:18:28 -0500 |
parents | c58cd7e48db7 |
children | 92770d2c8438 |
files | cmd2.py example/exampleSession.txt flatten_lines.py |
diffstat | 3 files changed, 116 insertions(+), 57 deletions(-) [+] |
line wrap: on
line diff
--- a/cmd2.py Thu Feb 11 13:07:05 2010 -0500 +++ b/cmd2.py Thu Feb 11 14:18:28 2010 -0500 @@ -271,52 +271,62 @@ exc.pstr = instring raise exc -class TextLineList(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. +class StubbornDict(dict): + '''Dictionary that tolerates many input formats. + Create it with stubbornDict(arg) factory function. - >>> tll = TextLineList(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']]) - >>> tll - ['my', 'dog', 'has', 'fleas', 'and', 'ticks'] - >>> tll.append(['and', ['spiders', 'and']]) - >>> tll - ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and'] - >>> tll += 'fish' - >>> tll - ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and', 'fish'] - ''' - def flattened(self, texts): - result = [] - if isinstance(texts, basestring): - result.extend(texts.splitlines()) + >>> 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 = '' + result[key] = val else: - for text in texts: - result.extend(self.flattened(text)) - result = [r.strip() for r in result if r.strip()] - return result - def flatten(self): - list.__init__(self, self.flattened(self)) - def __init__(self, values): - list.__init__(self, values) - self.flatten() - def append(self, value): - list.append(self, value) - self.flatten() - def extend(self, values): - list.extend(self, values) - self.flatten() - def __setitem__(self, idx, value): - list.__setitem__(self, idx, value) - self.flatten() - def __iadd__(self, value): - if isinstance(value, basestring): - self.append(value) - else: - list.__iadd__(self, value) - self.flatten() - return self + 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) def replace_with_file_contents(fname): if fname: @@ -383,7 +393,7 @@ feedback_to_output = False # Do include nonessentials in >, | output quiet = False # Do not suppress nonessential output debug = True - settable = TextLineList(''' + settable = stubbornDict(''' prompt colors Colorized output (*nix only) continuation_prompt @@ -472,12 +482,9 @@ self.shortcuts = sorted(self.shortcuts.items(), reverse=True) self.keywords = self.reserved_words + [fname[3:] for fname in dir(self) if fname.startswith('do_')] - import pdb; pdb.set_trace() def linelist(arg): result = [] - self.settable = (l.strip() for l in self.settable if l.strip()) - self.settable = dict(ljust(l.split(None,1), 2, '') for l in self.settable) self.doubleDashComment = pyparsing.NotAny(pyparsing.Or(options_defined)) + pyparsing.Literal('--') + pyparsing.restOfLine def do_shortcuts(self, args):
--- a/example/exampleSession.txt Thu Feb 11 13:07:05 2010 -0500 +++ b/example/exampleSession.txt Thu Feb 11 14:18:28 2010 -0500 @@ -30,6 +30,7 @@ (Cmd) set abbrev: True case_insensitive: True +colors: True continuation_prompt: > debug: True default_file_name: command.txt @@ -52,27 +53,20 @@ (Cmd) hi -------------------------[1] help - -------------------------[2] help say - -------------------------[3] say goodnight, Gracie - -------------------------[4] say -ps --repeat=5 goodnight, Gracie - -------------------------[5] set - -------------------------[6] set maxrepeats 5 - -------------------------[7] say -ps --repeat=5 goodnight, Gracie (Cmd) run 4 say -ps --repeat=5 goodnight, Gracie - OODNIGHT, GRACIEGAY OODNIGHT, GRACIEGAY OODNIGHT, GRACIEGAY
--- 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']])