Have you ever needed to parse string representations of Python data types. Lists, dictionaries, tuples? What about when the string is from an untrusted source and you need to parse it safely. My friend John Berninger wrote ConstructParser.py for this situation. For quite a while now it has been used in Current for parsing information sent from the client to the Current server. This code uses no eval() statements and raises one of several exceptions if the syntax of the string does not match a python representation.

Usage:

>>> from ConstructParser import \*\
>>> c = ConstructParser("{'foo':['bar', 2, 'baz']}")\
>>> c.parseIt()\
{'foo': ['bar', 2, 'baz']}\
>>>

Previous   Up   Next


comments powered by Disqus