On May 19, 12:32 am, Paddy <[EMAIL PROTECTED]> wrote:
> On May 16, 6:58 pm, "Hugo Ferreira" <[EMAIL PROTECTED]> wrote:
>
> > Hi!
>
> > Is it possible to "automagically" coerce the named groups to python types? 
> > e.g.:
>
> > >>> type(re.match('(?P<x>\d*)', '123').groupdict()['x'])
>
> > <type 'str'>
>
> > But what I'm looking forward is for the type to be 'int'.
>
> > Cheers!
>
> > Hugo Ferreira
>
> If you do a ot of that sort of thing in many programs
> then it might be worth your while to set up a framework
> that does it. Something like adding an underscore
> then the name of a type conversion function to all
> group names, and creating a function to apply the
> type convertion function to all named groups of a
> match object.
> - Paddy.

pyparsing might just be this sort of framework, in that you can attach
parse actions to elements within a grammar.  At parse time, the parse
action is called with the list of tokens currently matched.

>>> from pyparsing import Regex
>>> re = Regex( r"(\d*)" ).setResultsName("x")\
...        .setParseAction(lambda t:int(t[0]))
>>> results = re.parseString("123")
>>> print results.x
123

-- Paul

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to