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


Groups > comp.lang.python > #50728

grimace: a fluent regular expression generator in Python

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <benlast@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.072
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; 'subject:Python': 0.06; 'init': 0.07; 'welcome.': 0.07; '__init__': 0.09; 'url:github': 0.09; 'cheers': 0.12; 'executed.': 0.16; 'fluent': 0.16; 'subject:expression': 0.16; 'subject:generator': 0.16; 'subject:regular': 0.16; 'module': 0.19; 'normally': 0.19; 'import': 0.22; 'tests': 0.22; 'number)': 0.24; "i've": 0.25; 'compiled': 0.26; 'post': 0.26; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'comments': 0.31; 'regular': 0.32; 'cases': 0.33; 'minimal': 0.33; "i'd": 0.34; "who's": 0.35; 'received:google.com': 0.35; 'explains': 0.36; '8bit%:4': 0.38; 'ben': 0.38; 'mine': 0.38; 'to:addr:python-list': 0.38; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; '8bit%:29': 0.60; 'expression': 0.60; 'first': 0.61; 'here:': 0.62; 'such': 0.63; 'provide': 0.64; 'more': 0.64; 'north': 0.65; 'phone': 0.66; 'american': 0.66; 'header:Reply-To:1': 0.67; 'skip:r 40': 0.68; 'reply-to:no real name:2**0': 0.71; 'friend': 0.79; 'as:': 0.81; 'skip:. 50': 0.84; 'skip:. 60': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:from:date:message-id:subject:to:content-type; bh=BJxH0X4hzqIO9uh9T3GifWP0uNoEelZq5OeVgEjWMz8=; b=SgJPmXsHaKVI4JGGktFSa2wIEDfk0z1Mp6nY7W4/PWcDxg2/fE/nNsLCepfG2kVvA5 CzgzufzmurZq8YUKxsb4Egt6pJpdUSqBdQuXQuyEIkdngBut9t7UfLNJ7zseEPUjNN0s Q3WVoi1zHAVaQWrTKduOhwxE8Qp3l3zZkpnykCMDDZD3uKGHJhwKp8DsbDJtZK0xx+6B cTG3ZXs9Gw3sEzj4xSEOSvivAYu3+ExV15myW1YxkwdjJaODTmxOiUN4hGRjAElCW8kR i0KO3CoyGKLRT4DCekO1PtvV3mDC66PVTIzcNOLkK5dZ5Nxm1xJn+Y3VbW5DcvJ1G4/j 47Qw==
X-Received by 10.194.11.72 with SMTP id o8mr33925296wjb.0.1373926885427; Mon, 15 Jul 2013 15:21:25 -0700 (PDT)
MIME-Version 1.0
From Ben Last <benlast@gmail.com>
Date Tue, 16 Jul 2013 06:21:05 +0800
Subject grimace: a fluent regular expression generator in Python
To python-list@python.org
Content-Type multipart/alternative; boundary=e89a8f234d49f411f104e1944714
X-Mailman-Approved-At Tue, 16 Jul 2013 09:43:20 +0200
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To ben@benlast.com
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.4758.1373960601.3114.python-list@python.org> (permalink)
Lines 88
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373960601 news.xs4all.nl 15881 [2001:888:2000:d::a6]:34515
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50728

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi all

I'd be interested in comments on a fluent regular expression generator I've
been playing with (inspired by the frustrations of a friend of mine who's
learning).

The general use case is to be able to construct RE strings such as:

r'^\(\d{3,3}\)-{1,1}\d{3,3}\-{1,1}\d{4,4}$' (intended to match North
American phone number)

as:

from grimace import RE
north_american_number_re = (RE().start

.literal('(').followed_by.exactly(3).digits.then.literal(')')

.then.one.literal("-").then.exactly(3).digits

.then.one.dash.followed_by.exactly(4).digits.then.end
                                    .as_string())

The intent is to provide clarity: since the strings would normally be
generated and compiled when a module is first imported, there's minimal
overhead.

It's on github at https://github.com/benlast/grimace and the first blog
post that explains it is here: http://benlast.livejournal.com/30871.html (I've
added to it since then).

Tests are the best explanation, and they're in the __init__ at the end so
that they're self-executing if the init is executed.

I'm thinking about other features to implement, and more use cases would be
welcome.

Cheers
ben

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


Thread

grimace: a fluent regular expression generator in Python Ben Last <benlast@gmail.com> - 2013-07-16 06:21 +0800

csiph-web