Inverse of id()?
Paul Boddie
paul at boddie.org.uk
Mon May 21 05:55:19 EDT 2007
On 20 May, 01:42, Paul McGuire <p... at austin.rr.com> wrote:
>
> >>> re = Regex("(\d*)").setResultsName("x").setParseAction(lambda t:int(t[0]))
> >>> results = re.parseString("123")
>
> pyparsing results have some special lookup behavior, that if the
> results name is a Python-friendly identifier, can use the name as if
> it were an object attribute:
>
> >>> results.x
> 123
First of all, having recently looked at pyparsing again, I must say
that it's a nice framework for writing parsers quickly. Now I'm not
sure what the intention is behind this inquiry, so the following may
seem somewhat tangential, but one thing that I tend to do a lot with
pyparsing is to automatically set the results name by having a special
grammar object:
class Grammar:
def __setattr__(self, name, value):
self.__dict__[name] = Group(value.setResultsName(name))
This permits stuff like the following:
g = Grammar()
g.x = Regex("(\d*)").setParseAction(lambda t:int(t[0]))
You'd even be able to incorporate the parse action, too, with some
extra magic. As pyparsing is a library which seems to encourage clean
grammar definitions, I think this makes quite a difference, although I
now expect to be told that there's a class in the library which
supports this.
Anyway, back to the scheduled programme...
Paul
More information about the Python-list
mailing list