Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26137

regexps to objects

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '__name__': 0.07; 'assign': 0.07; 'constructor': 0.07; 'problem?': 0.07; 'type,': 0.07; 'def': 0.10; 'to:name:python-list': 0.15; "'__main__':": 0.16; 'input': 0.18; 'module': 0.19; 'import': 0.21; 'parse': 0.22; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'objects': 0.29; 'sense': 0.31; 'print': 0.32; 'to:addr:python-list': 0.33; '(with': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'received:209': 0.37; '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
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=+OgyCbSpD8g8bzRwMQMrUKHUt1QE3c4m/Boqq4B9c0c=; b=YDfiPPX9RbNeRAKMDfUm4Yj3QYDqDBbhYRroxia2aaKLq77xe8q+n1oQ6R/IOqzqEk vrTyQ1YCRe6hQhO5QdF9+5YRcikhr3quzCedNvuw2BJnZxbna+sGYhy4iSnRKMO8oPAy swW8VfSMnOmrU22oJTZC1SM68VyAhsL5hewWPm+dHMpeInG7yA2AiDJT45n0T8atw5Jr S26IJiuUORD82QCz+PpYuy+rePWSZXl0E94A+Wrnr4Rro2cQMPx3MLj732+leI5Eynud qoQ0lqvsaZ1r+BS6YRjA7rFY+Tp9wPhB8g5Ld0hdV1S5rBFWswww+A1TDYoEmB1r4lze E83A==
MIME-Version 1.0
Date Fri, 27 Jul 2012 10:36:34 +0100
Subject regexps to objects
From andrea crotti <andrea.crotti.0@gmail.com>
To python-list <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2651.1343381802.4697.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1343381802 news.xs4all.nl 6867 [2001:888:2000:d::a6]:56571
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:26137

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

regexps to objects andrea crotti <andrea.crotti.0@gmail.com> - 2012-07-27 10:36 +0100

csiph-web