Typed named groups in regular expression

M

Miki

Hello Hugo,
Is it possible to "automagically" coerce the named groups to python types? e.g.:
Not that I know of, however I use the following idiom:

match = my_regexp.find(some_string)
def t(name, convert=str):
return convert(match.group(name))

myint = t("field1", int)

HTH,
 
P

Paddy

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.
 
P

Paul McGuire

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'.

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
 
H

Hugo Ferreira

Both Paddy (hackish) and McGuire (right tool for the job) ideas sound
very interesting ;-) I'll definitely research on them further.

Thanks for the support...

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'.

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
 
P

Paddy

Both Paddy (hackish) and McGuire (right tool for the job) ideas sound
very interesting ;-) I'll definitely research on them further.

Thanks for the support...

Hackis, hackISH!
Sir, I would have you know that the idea proffered is a hack in the
finest traditions of hackerdom.

Why, the solution is guaranteed to work for the longest time with the
smallest expenditure and be delivered in the quickest time and be good
enough - but no more.

So, promote the idea to a hack or its pistols at dawn!
:)

- Paddy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top