Version 0.1
April 16, 1999
Neil Schemenauer <nascheme at ucalgary.ca>

This extension module parses Python literals from a file. An intended
use is for human readable data storage (configuration files, data
files, etc.). It is copyrighted under a Python style license.

The format of the input is the same as Python (with some extensions).
Lists, tuples, dictionaries, floats, integers, strings, and comments
are recognized. Triple quoted strings are not recognized. Long
integers and complex numbers are not recognized. Numbers are returned
as integers if possible. All sequences are returned as lists.

The extensions to the syntax are:

= is accepted in place of : for dictionaries.

Commas separating items are optional.

Quotes may be left off strings if they do not include any of the
following characters " \\t\\n#[](){}:=".

The LParser.parse method takes an optional parameter specifices parsing
start mode. If it is LParser.DICT then the parser behaves as if the input
is wrapped in { and }. The option LParser.LIST is similar except
the input is wrapped in [ and ]. Finally, the option LParser.VALUE
indicates that no wrapping should be done. In this case all input may
not be read. The value None is returned for end of file. The default
start mode is LParser.DICT.

A example data file using these extensions looks like this:

option1 = 10
option2 = (1 2 3)

# some useful commentary
subsystem1 = {
option1 = 10.102 # a little comment
option2 = "This is a quoted string"
good_option = on

when parsed that the following dictionary is returned:

{'option1': 10, 'subsystem1': {'option1': 10.102, 'good_option': 'on',
'option2': 'This is a quoted string'}, 'option2': [1, 2, 3]}

On a parse error, an LParserError exception is raised. The value of
the exception gives the line number and a short description of what
when wrong.


nulls handled correctly
no arbitrary limits
fast (compared to eval)
safe (compared to eval)

Known Bugs:

[ ... ) and ( ... ] are treated as valid lists.
String escape processing may not be the same as Python.
\r may not be handled correctly.
Nulls in input can affect error reporting.
Error reporting is somewhat limited.
Data must come from a real file, not a StringIO etc.



Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 1 | next ›
Discussion Overview
grouppython-list @
postedApr 16, '99 at 10:30p
activeApr 16, '99 at 10:30p

1 user in discussion

Neil Schemenauer: 1 post



site design / logo © 2022 Grokbase