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:

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.

Please enable JavaScript to view the comments powered by Disqus.

Disqus