Inverse of id()?

Paul McGuire ptmcg at austin.rr.com
Wed May 23 16:56:59 EDT 2007


On May 21, 4:55 am, Paul Boddie <p... at boddie.org.uk> wrote:
> 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

I'm considering adding a notational short-cut as an abbreviated
version of setResultsName(), in the next pyparsing release, described
here: http://pyparsing.wikispaces.com/message/view/home/606302
I propose we move the discussion of this idea to the pyparsing wiki,
so as not to (further?) clutter up c.l.py with this topic.

-- Paul




More information about the Python-list mailing list