<div class="gmail_quote">On Fri, Feb 27, 2009 at 5:14 AM, Gabriel Genellina <span dir="ltr"><<a href="mailto:gagsl-py2@yahoo.com.ar" target="_blank">gagsl-py2@yahoo.com.ar</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I think a r.e. cannot handle this query well enough.<br>
This script uses the tokenize module and should be immune to those false<br>
positives (but it's much slower)<br>
</blockquote></div><br>Thanks, Gabriel. Using the tokenize module is, indeed, much better. I think the performance problems were caused by the O(n**2) cost of reading through the directories and then removing them selectively. I modified it to have an O(n) cost and it's quite snappy.<br>
<br>#!/usr/bin/python<br>from __future__ import with_statement<br><br>import sys, os<br>from tokenize import generate_tokens<br>from token import NAME<br><br>def process(paths):<br> nfiles = nwith = ntrywith = 0<br> for path in paths:<br>
for base, dirs, files in os.walk(path):<br> if nfiles:<br> print '%d "try+with" out of %d "with" (%.1f%%) in %d files (so far)\<br>'% (ntrywith, nwith, ntrywith*100.0/nwith if nwith else 0, nfiles)<br>
print base<br><br> newdirs = []<br> for d in list(dirs):<br> if d == 'CVS' or d == '_darcs' or d[0] == '.': continue<br> newdirs.append(d)<br> dirs[:] = newdirs<br>
for fn in files:<br> if fn[-3:]!='.py': continue<br> fullfn = os.path.join(base, fn)<br> #print fn<br> nfiles += 1<br> with open(fullfn) as f:<br> try:<br>
was_try = False<br> for toknum, tokval, _, _, _ in generate_tokens(f.readline):<br> if toknum==NAME:<br> is_with = tokval == 'with'<br> if is_with:<br>
nwith += 1<br> if was_try: ntrywith += 1<br> was_try = tokval == 'try'<br> except Exception, e: print e<br><br> print '%d "try+with" out of %d "with" (%.1f%%) in %d files' % (<br>
ntrywith, nwith, ntrywith*100.0/nwith if nwith else 0,<br> nfiles)<br><br>process(sys.argv[1:])<br><br>
<br><blockquote style="margin: 1.5em 0pt;">--<br>
Daniel Stutzbach, Ph.D.<br>
President, <a href="http://stutzbachenterprises.com">Stutzbach Enterprises, LLC</a>
</blockquote>