Eval (was Re: Question about using python as a scripting language)

Chris Lambacher chris at kateandchris.net
Wed Aug 9 11:21:52 EDT 2006


There were some mistakes in here.  Thats what I get for repurposing existing
code for an example.  The uncommented lines are changed.

On Wed, Aug 09, 2006 at 11:04:32AM -0400, Chris Lambacher wrote:
from pyparsing import Suppress, Regex, delimitedList, Forward, QuotedString, Group
> 
> stringValue = QuotedString('"', '\\', multiline=True)
> 
> intValue = Regex(r'[+-]?0|([1-9][0-9]*)')
> intValue.setParseAction(lambda s,l,toks: int(toks[0]))
> 
> floatValue = Regex(r'[+-]?[0-9]+\.[0-9]+([eE][+-]?[0-9]+)?')
floatValue.setParseAction(lambda s,l,toks: float(toks[0]))
> constantValue = stringValue ^ intValue ^ floatValue
> arrayInitializer = Forward()
arrayInitializer << (Suppress("[") + delimitedList(constantValue ^ Group(arrayInitializer)) \
                         + Suppress("]"))
> arrayInitializer.setParseAction(lambda s,l,toks: toks.asList())
> 
> test_data = '["a", 1, 5.6, ["b","c",6, 10.0e-19]]'
> print arrayInitializer.parseString(test_data)
> 
> results in:
['a', 1, 5.5999999999999996, ['b', 'c', 6, 1.0000000000000001e-18]]



More information about the Python-list mailing list