Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

Experiment: Adding "re" to string objects.

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


jafo at tummy

Jul 17, 2009, 5:28 PM

Post #1 of 4 (606 views)
Permalink
Experiment: Adding "re" to string objects.

I'm mailing this to python-dev because I'd like feedback on the idea of
adding an "re" attribute to strings. I'm not sure if it's a good idea or
not yet, but I figure it's worth discussion. The module mentioned here
includes a class called "restr()" which allows you to play with "s.re".

As some of you may recall, I'm not particularly fond of the recipe:

m = re.match(r'whatever(.*)', s)
if m:
m.group(1)

The other morning I came up on the idea of adding an "re" to strings, so
you could do things like:

if s.re.match(r'whatever(.*)'):
print s.re.group(1)

or:

if (date.re.match(r'(?P<year>\d\d\d\d)-(?P<month>\d\d)' or
date.re.match(r'(?P<month>\d\d)-(?P<year>\d\d\d\d)'):
print date.re.groupdict('year')

So I decided to try experimenting with it and see how I like it. I've also
thrown a bunch of other stuff into it and made a module called
"filtertools":

http://pypi.python.org/pypi/filtertools/0.01
ftp://ftp.tummy.com/pub/tummy/Python/python-filtertools/

As the version number is meant to indicate, this is something that I'm
still exploring whether it is the right thing done in the right way.
Though at the moment the only thing I plan to change is that some of the
iterators (having nothing to do with adding "re" to string objects)
probably shouldn't consume the "barrier" such as the "dropwhile()" and
"takewhile()". You might want to do something like:

fp = filtertools.reopen('mailbox')
for header in filtertools.takewhile([ r'^\S' ], fp.readlines()) :
print 'HEADER:', header.rstrip()
for continued in filtertools.takewhile([ r'^\s+\S' ], fp.readlines()) :
print 'CONTINUED:', continued.rstrip()

But, the "takewhile()" I will consume the first non-matching line.

Anyway, I appreciate any feedback folks have.

Thanks,
Sean
--
What we see depends on mainly what we look for.
-- John Lubbock
Sean Reifschneider, Member of Technical Staff <jafo [at] tummy>
tummy.com, ltd. - Linux Consulting since 1995: Ask me about High Availability

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


aahz at pythoncraft

Jul 17, 2009, 5:48 PM

Post #2 of 4 (553 views)
Permalink
Re: Experiment: Adding "re" to string objects. [In reply to]

On Fri, Jul 17, 2009, Sean Reifschneider wrote:
>
> I'm mailing this to python-dev because I'd like feedback on the idea of
> adding an "re" attribute to strings. I'm not sure if it's a good idea or
> not yet, but I figure it's worth discussion. The module mentioned here
> includes a class called "restr()" which allows you to play with "s.re".

Ideas should go to python-ideas, please.
--
Aahz (aahz [at] pythoncraft) <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur." --Red Adair
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


python at mrabarnett

Jul 17, 2009, 5:51 PM

Post #3 of 4 (546 views)
Permalink
Re: Experiment: Adding "re" to string objects. [In reply to]

Sean Reifschneider wrote:
> I'm mailing this to python-dev because I'd like feedback on the idea of
> adding an "re" attribute to strings. I'm not sure if it's a good idea or
> not yet, but I figure it's worth discussion. The module mentioned here
> includes a class called "restr()" which allows you to play with "s.re".
>
> As some of you may recall, I'm not particularly fond of the recipe:
>
> m = re.match(r'whatever(.*)', s)
> if m:
> m.group(1)
>
> The other morning I came up on the idea of adding an "re" to strings, so
> you could do things like:
>
> if s.re.match(r'whatever(.*)'):
> print s.re.group(1)
>
> or:
>
> if (date.re.match(r'(?P<year>\d\d\d\d)-(?P<month>\d\d)' or
> date.re.match(r'(?P<month>\d\d)-(?P<year>\d\d\d\d)'):
> print date.re.groupdict('year')
>
[snip]
Why not drop the ".re" part? You would, however, then need a new name
for the re split, eg "re_split".

Or you could make the string the pattern, eg r'whatever(.*)'.match(s).
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


fuzzyman at voidspace

Jul 18, 2009, 4:21 AM

Post #4 of 4 (545 views)
Permalink
Re: Experiment: Adding "re" to string objects. [In reply to]

MRAB wrote:
> Sean Reifschneider wrote:
>> I'm mailing this to python-dev because I'd like feedback on the idea of
>> adding an "re" attribute to strings. I'm not sure if it's a good
>> idea or
>> not yet, but I figure it's worth discussion. The module mentioned here
>> includes a class called "restr()" which allows you to play with "s.re".
>>
>> As some of you may recall, I'm not particularly fond of the recipe:
>>
>> m = re.match(r'whatever(.*)', s)
>> if m:
>> m.group(1)
>>
>> The other morning I came up on the idea of adding an "re" to strings, so
>> you could do things like:
>>
>> if s.re.match(r'whatever(.*)'):
>> print s.re.group(1)
>>
>> or:
>>
>> if (date.re.match(r'(?P<year>\d\d\d\d)-(?P<month>\d\d)' or
>> date.re.match(r'(?P<month>\d\d)-(?P<year>\d\d\d\d)'):
>> print date.re.groupdict('year')
>>
> [snip]
> Why not drop the ".re" part? You would, however, then need a new name
> for the re split, eg "re_split".
>
> Or you could make the string the pattern, eg r'whatever(.*)'.match(s).

+1 for re support built-in to strings.

Michael Foord

> _______________________________________________
> Python-Dev mailing list
> Python-Dev [at] python
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk
>


--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog


_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.