Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!cs.uu.nl!news0.firedrake.org!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; "'')": 0.07; '__name__': 0.07; 'assign': 0.07; 'constructor': 0.07; 'problem?': 0.07; 'type,': 0.07; 'valueerror:': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'undocumented': 0.09; 'def': 0.10; "'__main__':": 0.16; '24,': 0.16; 'lambda': 0.16; 'message- id:@dough.gmane.org': 0.16; 'module:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'input': 0.18; 'module': 0.19; 'skip:" 30': 0.20; 'import': 0.21; '"",': 0.22; 'parse': 0.22; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; '(most': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; 'objects': 0.29; 'class': 0.29; 'sense': 0.31; 'file': 0.32; 'print': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; '(with': 0.33; 'skip:d 20': 0.34; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'does': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'build': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'below,': 0.60; 'worth': 0.63; 'andrea': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: regexps to objects Date: Fri, 27 Jul 2012 12:24:41 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b332.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343384695 news.xs4all.nl 6852 [2001:888:2000:d::a6]:38551 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26139 andrea crotti wrote: > 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') 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 "", line 1, in File "/usr/lib/python2.7/re.py", line 322, in scan action = action(self, m.group()) File "", line 2, in 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'