>
> is it possible to do sth. like .setResultsName globally and automatically?
E.g.:
>
> a = pyparsing.Group(b + c)
>
> could work like:
>
> a = pyparsing.Group(b + c).setResultsName("a")
>
Results names are tricky, but useful. Part of the nuisance of results names
is the ".setResultsName" syntax, so that is why in 1.4.7 I added support for
code like this:
a = pyparsing.Group(b + c)("a")
That is, I made all ParserElements callable, and __call__ calls
setResultsName.
Also, despite the leading "set" in "setResultsName", you do know I hope that
this method does an implicit copy, and the copy gets the results name, not
the original. This is so that you can create a basic expression like
integer, and then use it in multiple places with different names, as in:
integer = Word(nums)
personData = Word(alphas)("name") + integer("age") + integer("salary")
(This actually looks almost like a constructor syntax, which is consistent
with the concept that we are creating new sub-expressions, using the
definition of integer as a prototype.) But you don't really want integer to
have a results name of "integer", since you need different names for the
different data items, so automatic naming, which seems like a good idea at
first, doesn't hold up - sometimes it would be helpful, but other times it
gets in your way.
If you had a grammar with many sub-expressions, you could do something like
this (where '...' is whatever pyparsing definition you want):
Aexpr = ...
Bexpr = ...
Cexpr = ...
grammar = (Aexpr | Bexpr) + OneOrMore(Cexpr)
for exprName in ("Aexpr Bexpr Cexpr".split()):
# note that this won't work, since it makes and then discards a copy of
the original
# locals()[exprName].setResultsName(exprName)
# have to set the attribute directly, like this
locals()[exprName].resultsName = exprName
This might be more in line with what you were asking. I've never set an
expression's results name this way, but I don't see anything wrong with it.
-- Paul
|