Don't you just love writing this sort of thing :)

Duncan Booth duncan.booth at invalid.invalid
Thu Dec 4 04:01:30 EST 2008


Lawrence D'Oliveiro <ldo at geek-central.gen.new_zealand> wrote:

> for \
>         Entry \
>     in \
>         sorted \
>           (
>             f for f in os.listdir(PatchesDir) if
>             PatchDatePat.search(f) != None 
>           ) \
>:
>     Patch = (open,
>     gzip.GzipFile)[Entry.endswith(".gz")](os.path.join(PatchesDir,
>     Entry), "r") ... read from Patch ...
>     Patch.close()
> #end for
> 

Have you ever considered trying to write readable code instead?

Something like (untested):

def patchfiles(dir, pattern):
   for f in os.listdir(dir):
       if pat.search(f) is not None:
          yield os.path.join(dir, f)

def openpatch(name):
   if name.endswith(".gz"):
      return gzip.GzipFile(name, "r")
   return open(name, "r")

for entry in sorted(patchfiles(PatchesDir, PatchDatePat):
   with openpatch(entry) as patch:
      ... read from patch ...
    
(I must admit I haven't checked whether GZipFile works with the 'with' 
statement, but if it doesn't you just wrap it in contextlib.closing).

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list