From: Paul McGuire <paul@al...> - 2007-06-04 16:55:19
Ok, I didn't really get any takers on using 's as a notation short-cut for
setResultsName, but I'd like to give this another shot. How does this
from pyparsing import *
realValue = Combine( Word(nums) + "." + Word(nums) )\
stats = "AVE:" + realValue("average") + \
"MIN:" + realValue("min") + \
"MAX:" + realValue("max")
statsResults = stats.parseString("AVE: 10.0 MIN: 5.7 MAX: 14.1")
This prints out:
['AVE:', 10.0, 'MIN:', 5.7000000000000002, 'MAX:', 14.1]
- average: 10.0
- max: 14.1
- min: 5.7
This still fails J-P Calderone's explicitness test, but perhaps using ()'s
instead of 's might alleviate some of the other concerns about
misinterpreting this as some sort of "getter" operation. Of the other
responses from readers of the first proposal, here is a summary:
- add results name to the expression constructor - I documented my
objection to this suggestion, since I want expressions to be composed
separately from their results names, which allows a generic expression (such
as "realValue" in the example) to be used multiple times, but returning
different results names
- use ._, as in realValue._("average") - I did not really respond to this
suggestion, but I'm not trying to define a synonym for setResultsName (and
while "_" is blissfully short, it doesn't evoke results naming any more than
it might invoke setting a parse action or enabling debug)
I guess this is really the crux for me - I'm *not* trying to define a
synonym for setResultsName, I'm trying to extend pyparsing's notation (as a
DSL within Python, perhaps?) so that *notationally*, something expressed as
one of these:
1) stats = "AVE:" + realValue["average"]
2) stats = "AVE:" + realValue("average")
3) stats = "AVE:" + realValue ** "average"
4) stats = "AVE:" + realValue * "average"
5) stats = "AVE:" + realValue / "average"
6) stats = "AVE:" + realValue % "average"
could be learned as the notation for "set the tokens matched for the
realValue to have the name 'average' in the returned results". I guess it's
a subtle distinction, but I think a big part of pyparsing's appeal is the
visual representation of the grammar, using logical-looking operators such
as '+' and '|', and less so that these operators are actually implemented
using __add__ and __or__.
I like the symmetry of option #1 with the notation for retrieving the
results, but the only responses I got on this option indicated a confusion
with the concept of "item getting". The option #2 is my second choice,
since it could be notationally interpreted as a parenthetical aside: "after
AVE: follows a realValue (named 'average')". Option #3 '**' does not overly
thrill me, and Options #4-6 have the issue that '*', '/' and '%' have lower
precedence than '~' which could lead to expression confusion (a lesson I
learned when choosing '<<' instead of '<<=' as the contents insertion
operator for the Forward class).
Thanks for your comments and feedback!