Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
regexps to objects
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
andrea crotti  
View profile  
 More options Jul 27 2012, 5:36 am
Newsgroups: comp.lang.python
From: andrea crotti <andrea.crott...@gmail.com>
Date: Fri, 27 Jul 2012 10:36:34 +0100
Local: Fri, Jul 27 2012 5:36 am
Subject: regexps to objects
I have some complex input to parse (with regexps), and I would like to
create nice objects directy from them.
The re module doesn't of course try to conver to any type, so I was
playing around to see if it's worth do something as below, where I
assign a constructor to every regexp and build an object from the
result..

Do you think it makes sense in general or how do you cope with this problem?

import re
from time import strptime
TIME_FORMAT_INPUT = '%m/%d/%Y %H:%M:%S'

def time_string_to_obj(timestring):
    return strptime(timestring, TIME_FORMAT_INPUT)

REGEXPS = {
    'num': ('\d+', int),
    'date': ('[0-9/]+ [0-9:]+', time_string_to_obj),

}

def reg_to_obj(reg, st):
    reg, constr = reg
    found = re.match(reg, st)
    return constr(found.group())

if __name__ == '__main__':
    print reg_to_obj(REGEXPS['num'], '100')
    print reg_to_obj(REGEXPS['date'], '07/24/2012 06:23:13')


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Jul 27 2012, 6:24 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Fri, 27 Jul 2012 12:24:41 +0200
Local: Fri, Jul 27 2012 6:24 am
Subject: Re: regexps to objects

There is an undocumented Scanner class in the re module:

>>> from datetime import datetime
>>> from re import Scanner
>>> sc = Scanner([

... ("[0-9/]+ [0-9:]+", lambda self, s: datetime.strptime(s, "%m/%d/%Y %H:
%M:%S")),
... (r"\d+", lambda self, s: int(s)),
... ("\s+", lambda self, s: None)])

>>> sc.scan("07/24/2012 06:23:13")

([datetime.datetime(2012, 7, 24, 6, 23, 13)], '')
>>> sc.scan("07/24/2012 06:23:13 123")

([datetime.datetime(2012, 7, 24, 6, 23, 13), 123], '')

However:

>>> sc.scan("456 07/24/2012 06:23:13 123")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/re.py", line 322, in scan
    action = action(self, m.group())
  File "<stdin>", line 2, in <lambda>
  File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data '456 07' does not match format '%m/%d/%Y %H:%M:%S'

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »