
En Fri, 27 Feb 2009 00:00:59 -0200, Daniel Stutzbach <daniel-k6jgQluAQ1RLuhoIm4CQ5lNBC6VQM+k9@public.gmane.org> escribió:
On Thu, Feb 26, 2009 at 7:28 PM, Bruce Frederiksen <dangyogi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:
How about this? I tried it, but it matches quite a few comments and strings. Try
I think a r.e. cannot handle this query well enough. This script uses the tokenize module and should be immune to those false positives (but it's much slower) <code> import sys, os from tokenize import generate_tokens from token import NAME def process(path): nfiles = nwith = ntrywith = 0 for base, dirs, files in os.walk(path): print base print '%d "try+with" out of %d "with" (%.1f%%) in %d files (partial)' % ( ntrywith, nwith, ntrywith*100.0/nwith if nwith else 0, nfiles) print for fn in files: if fn[-3:]!='.py': continue if 'CVS' in dirs: dirs.remove('CVS') if '.svn' in dirs: dirs.remove('.svn') fullfn = os.path.join(base, fn) #print fn nfiles += 1 with open(fullfn) as f: try: was_try = False for toknum, tokval, _, _, _ in generate_tokens(f.readline): if toknum==NAME: is_with = tokval == 'with' if is_with: nwith += 1 if was_try: ntrywith += 1 was_try = tokval == 'try' except Exception, e: print e print '%d "try+with" out of %d "with" (%.1f%%) in %d files' % ( ntrywith, nwith, ntrywith*100.0/nwith if nwith else 0, nfiles) process(sys.argv[1]) </code> I got 2/25 on my code (1500 files, but many are rather old to use "with") -- Gabriel Genellina