[Zope-dev] regex.compile used quite often

Jim Fulton jim@digicool.com
Sat, 12 Jun 1999 01:33:35 +0000


Eric Kidd wrote:
> 
> I've been reading through ZPublisher/HTTPRequest.py, and I noticed several
> uses of regex.compile. Oddly enough, the result is thrown out after one use
> and recomputed later.

Where do you see this?  I don't see any case of this.

Note that most usages are like:

  end_of_header_search=regex.compile('</head>',regex.casefold).search

Here we retain the regular expression's search method.  This effectively
keeps the regex around too.  By only keeping the search method (and not
the regex), it's easy to see that the regex usage is thread safe.

We also create a regex when gathering form input.  We may use it several times
before throwing it away.  We make a new regex on each call for thread
safety reasons. 

> Isn't regex.compile an expensive operation? 

Somewhat. It's not that big a deal.  I keep them around
most of the time.

> Should the results of
> regex.compile by stored between uses?

Absolutely. But one has to be careful about thread safety.
Using a regex search and group methods together can cause race
conditions.  

Jim

--
Jim Fulton           mailto:jim@digicool.com
Technical Director   (888) 344-4332              Python Powered!
Digital Creations    http://www.digicool.com     http://www.python.org

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.