From: James Stroud on
Hello All,

I want to use an s-expression based configuration file format for a
python program I'm writing. Does anyone have a favorite parser?

I'm currently using sexpy.parse() (http://pypi.python.org/pypi/sexpy)
which works very well but I don't know how secure it is. Does anyone
have experience with sexpy or another parser? I'm mostly concerned with
safety when the parser evaluates scalar data types. Sexpy is not well
documented in this regard.

Also, I don't want a lisp/scheme interpreter, just the parser.

For example,

"""
(and
(or (> uid 1000)
(!= gid 20)
)
(> quota 5.0e+03)
)
"""

Should be parsed to

["and",
["or", ["=", "uid", 1405],
["!=", "gid", 20]],
[">", "quota", 5000.0]
]

Note proper parsing of ints, floats, etc.

I've already wrote the evaluator for the parsed lists, so I'm not
interested in alternatives to S-expressions. Also, I'm really not
interested in json, yaml, etc., for the configuration format because I'm
quite fond of the brevity and clarity of S-expressions (your opinion may
vary).

Thanks in advance for any insight.

James
From: Patrick Maupin on
On Apr 6, 7:02 pm, James Stroud <nospamjstroudmap...(a)mbi.ucla.edu>
wrote:

I have a parser/emitter I wrote about 4 years ago for EDIF. Take a
look at the wikipedia article http://en.wikipedia.org/wiki/EDIF

If that is close to you want, I can send it to you. The whole parser/
emitter/XML round-tripper etc. is only 500 lines, which includes a
bunch of comments. Tiny.

I've never made a project out of it, but it worked fine for me and a
friend. (I have a testbench, but unfortunately the test data is
proprietary.)

But, I have to take this opportunity to put in a plug for a file
format I created for configuration files. It's secure (no eval
statements), it's much lighter weight than YAML, and it looks better
too:

http://code.google.com/p/rson/wiki/Manual

Please let me know if you'd like the edif code.

Best regards,
Pat
From: Paul McGuire on
On Apr 6, 7:02 pm, James Stroud <nospamjstroudmap...(a)mbi.ucla.edu>
wrote:
> Hello All,
>
> I want to use an s-expression based configuration file format for a
> python program I'm writing. Does anyone have a favorite parser?
>

The pyparsing wiki includes this parser on its Examples page:
http://pyparsing.wikispaces.com/file/view/sexpParser.py. This parser
is also described in more detail in the pyparsing e-book from
O'Reilly.

This parser is based on the BNF defined here:http://
people.csail.mit.edu/rivest/Sexp.txt. I should think Ron Rivest would
be the final authority on S-expression syntax, but this BNF omits '!',
'<', and '>' as valid punctuation characters, and does not support
free-standing floats and ints as tokens.

Still, you can extend the pyparsing parser (such is the goal of
pyparsing, to make these kinds of extensions easy, as the source
material or BNF or requirements change out from underneath you) by
inserting these changes:

real = Regex(r"[+-]?\d+\.\d*([eE][+-]?\d+)?").setParseAction(lambda
tokens: float(tokens[0]))
token = Word(alphanums + "-./_:*+=!<>")
simpleString = real | decimal | raw | token | base64_ | hexadecimal |
qString

And voila! Your test string parses as:

[['and',
['or', ['>', 'uid', 1000], ['!=', 'gid', 20]],
['>', 'quota', 5000.0]]]

-- Paul
 | 
Pages: 1
Prev: plotting in python 3
Next: lambda with floats