Package pythonutils :: Module listquote :: Class LineParser
[hide private]
[frames] | no frames]

Class LineParser
source code

object --+
         |
        LineParser

An object to parse nested lists from strings.

Instance Methods [hide private]
  __init__(self, options=None, **keywargs)
Initialise the LineParser.
  reset(self, options=None, **keywargs)
Reset the parser with the specified options.
  feed(self, inline, endchar=None)
Parse a single line (or fragment).

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__


Class Variables [hide private]
liststart  
quotes  

Inherited from object: __class__


Method Details [hide private]

__init__(self, options=None, **keywargs)
(Constructor)

source code 
Initialise the LineParser.
Overrides: object.__init__

reset(self, options=None, **keywargs)

source code 
Reset the parser with the specified options.

feed(self, inline, endchar=None)

source code 

Parse a single line (or fragment).

Uses the options set in the parser object.

Can parse lists - including nested lists. (If recursive is False then nested lists will cause a BadLineError).

Return value depends on options.

If comment is False it returns outvalue

If comment is True it returns (outvalue, comment). (Even if comment is just '').

If force_list is False then outvalue may be a list or a single item.

If force_list is True then outvalue will always be a list - even if it has just one member.

List syntax :

  • Comma separated lines a, b, c, d

  • Lists can optionally be between square or ordinary brackets
    • [a, b, c, d]
    • (a, b, c, d)
  • Nested lists must be between brackets - a, [a, b, c, d], c

  • A single element list can be shown by a trailing quote - a,

  • An empty list is shown by () or []

Elements can be quoted with single or double quotes (but can't contain both).

The line can optionally end with a comment (preeded by a '#'). This depends on the comment attribute.

If the line is badly built then this method will raise one of :

CommentError, BadLineError, UnQuoteError

Using the csv option is the same as setting :

'recursive': False
'force_list': True
'comment': False

Class Variable Details [hide private]

liststart

Value:
{'(': ')', '[': ']'}                                                   
      

quotes

Value:
["'", '"']