[Python-ideas] "try with" syntactic sugar

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Feb 27 12:14:28 CET 2009

En Fri, 27 Feb 2009 00:00:59 -0200, Daniel Stutzbach
<daniel-k6jgQluAQ1RLuhoIm4CQ5lNBC6VQM+k9 at public.gmane.org> escribió:
> On Thu, Feb 26, 2009 at 7:28 PM, Bruce Frederiksen  
> <dangyogi-Re5JQEeQqe8AvxtiuMwx3w at 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)

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,
       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:
             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,


I got 2/25 on my code (1500 files, but many are rather old to use "with")

Gabriel Genellina

More information about the Python-ideas mailing list