# HG changeset patch # User catherine@dellzilla # Date 1265911625 18000 # Node ID c58cd7e48db76dcd44bae940af4e8a194ea57f75 # Parent f44ad8de0d17681fabf6d619d0abf0e282ab4e7d begin to switch settable to TextLineList diff -r f44ad8de0d17 -r c58cd7e48db7 cmd2.py --- a/cmd2.py Thu Feb 11 11:25:35 2010 -0500 +++ b/cmd2.py Thu Feb 11 13:07:05 2010 -0500 @@ -271,6 +271,53 @@ 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. + + >>> 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()) + 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 + def replace_with_file_contents(fname): if fname: try: @@ -336,7 +383,7 @@ feedback_to_output = False # Do include nonessentials in >, | output quiet = False # Do not suppress nonessential output debug = True - settable = ''' + settable = TextLineList(''' prompt colors Colorized output (*nix only) continuation_prompt @@ -349,7 +396,7 @@ echo Echo command issued into output timing Report execution times abbrev Accept abbreviated commands - '''.splitlines() + ''') def poutput(self, msg): if msg: diff -r f44ad8de0d17 -r c58cd7e48db7 flatten_lines.py --- a/flatten_lines.py Thu Feb 11 11:25:35 2010 -0500 +++ b/flatten_lines.py Thu Feb 11 13:07:05 2010 -0500 @@ -1,19 +1,50 @@ import doctest -def flatten(texts): - ''' - >>> flatten([['cow', 'cat'], '', 'fish', ['bird']]) - ['cow', 'cat', 'fish', 'bird'] + +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. + + >>> 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'] ''' - result = [] - if isinstance(texts, basestring): - result.extend(texts.splitlines()) - else: - for text in texts: - result.extend(flatten(text)) - result = [r.strip() for r in result if r.strip()] - return result -doctest.testmod() - -class LineList(list): - def __add__(self, newmember): - + def flattened(self, texts): + result = [] + if isinstance(texts, basestring): + result.extend(texts.splitlines()) + 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 + +doctest.testmod() \ No newline at end of file