2006-02-22

Incoming python variant

Matt Palmer wrote up a ruby thingy, I'm going to quote him verbatim...

A method to find all files in a directory INCOMING which end with .changes, prepend INCOMING to the matching files, and return the results as an array (desk checked, but not run in anger yet):

def changes_files
Dir.entries(INCOMING).find_all { |f|
f =~ /\.changes$/ }.map { |f| File.join(INCOMING, f) }
end


Line noise it may be, but I'll be damned if it isn't succinct line noise.

I have a competitive streak a mile wide, so I feel compelled to present the Python version of this little doohickey.

import glob, os
def changes_files(incoming):
  return [os.path.abspath(f)
          for f in glob.glob(os.path.join(incoming, '*.changes'))]


I think I captured the specification, but I didn't implement the algorithm the same way. Also not used in anger, only very trivially tested. :)

On reflection, if relative paths are okay too, then this will work fine too:

import glob, os
def changes_files(incoming):
  return glob.glob(os.path.join(incoming, '*.changes'))


'rati tags:

1 comment:

Frederick Ros said...

Well, I would say the original ruby version is quite to long ...

def changes_files
Dir['INCOMING/*.changes']
end

achieve the same (finding all files .changes in INCOMING directory). Alternatively the following code, will find all .changes file in INCOMING and subdirectories:

def changes_files
Dir['INCOMING/**/*.changes']
end