<div dir="ltr">keep=True defeats the purpose of a caching strategy.  An re.compile call within some code somewhere is typically not in a position to know if it is going to be called a lot.<div><br></div><div style>I think the code, as things are now, with dynamic construction at runtime based on a simple test is the best of both worlds to avoid the more complicated cost of calling re.compile and going through its cache logic.  If the caching is ever is improved in the future to be faster, the code can arguably be simplified to use re.search or re.match directly and rely solely on the caching.</div>

<div style><br></div><div style>ie: don't change anything.</div><div style><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Mar 23, 2013 at 4:03 PM, Bruce Leban <span dir="ltr"><<a href="mailto:bruce@leapyear.org" target="_blank">bruce@leapyear.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">To summarize:<div><br></div><div>- compiling regexes is slow so applications frequently compute it once and save it</div>

<div>- compiling all the regexes at startup slows down startup for regexes that may never be used</div>

<div>- a common pattern is to compute once at time of use and it would be nice to optimize this pattern</div><div>- the regex library has a cache feature which means that frequently it will be optimized automatically</div>



<div>- however, there's no guarantee that the regex you care about won't fall out of the cache.</div><div><br></div><div>I think this addresses all the issues better than compute_lazy:</div><div><br></div><div>re.compile(r'...', keep=True)</div>



<div><br></div><div>When keep=True is specified, the regex library keeps the cached value for the lifetime of the process. The regex is computed only once on first use and you don't need to create a place to store it. Furthermore, if you use the same regex in more than one place, once with keep=True, the other uses will automatically be optimized.</div>



<div><div><br clear="all"><div><font face="arial, helvetica, sans-serif">--- Bruce</font><div><span style="font-family:arial,helvetica,sans-serif">Latest blog post: Alice's Puzzle Page </span><a href="http://www.vroospeak.com/" style="font-family:arial,helvetica,sans-serif" target="_blank">http://www.vroospeak.com</a></div>



<div><div><font face="arial, helvetica, sans-serif">Learn how hackers think: <a href="http://j.mp/gruyere-security" target="_blank">http://j.mp/gruyere-security</a></font></div></div></div>
<br><br></div></div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
<br></blockquote></div><br></div>