Friday July 17, 2009 at 19:02
Subject: Experiment: Adding "re" to Python string objects.
Keywords:
Filter, Python
Posted by: Sean Reifschneider
For a while I've had this idea stuck in my head that it would be nice
to have an "re" attribute on strings to tie them together. I mean, all the
re methods take a string, but then you have to keep a reMatch object around
to get access to the group() method... My idea is you could have something
like this:
(Post Reply)
if line.re.search(r'Date: (.*)'): print line.re.group(1)To experiment with this, I have created a module that implements a "restr()" class, including an "open()" wrapper, "readlines()" short-cut, and a bunch of itertools-inspired methods for processing files as an iterator over the lines of the file, as restr() objects. Read on for more information. I wrote up a description to the python-dev mailing list, because the right place for it, if it is the right solution, would be in the string objects. See the above link for the python-dev discussion. The module itself includes a lot of documentation in the doc-strings. Things that make this interesting to system admins are iterator tools like "grep()" and "grepv()" (grep -v), and "cat(file_or_iterator[, ...])". Thanks to a little help from Paul Hummer, I've created a Launchpad Project for filtertools. I plan to use this for the BZR hosting and bug-tracking. It's also available via PiPY.
(Post Reply)
| Comment |
David Subject: String mutability |
Wouldn't this make strings mutable?
| Comment |
L Subject: Why do I need a subject? |
I'd say the current way encourages you to compile your regular expressions, and thus wins some ground there.
| Comment |
rgz Subject: Mutable do not want |
Native regex or at least regex methods on strings is something I've always wanted but mutable strings is a big no.
| Comment |
Author:
Sean Reifschneider Subject: Mutability and compiled regexes. |
This change wouldn't make strings mutable, the string itself would remain unchanged. The string object would have methods associated with it that would change based on the regular expression that is applied to it, but the string would still be what it originally was.
As far as compiled regexes, they don't gain you much performance improvement over using string regexes because the re module will compile and cache it so the next time you use it you get the pre-compiled version.
Sean