def find_highest_files(directory, unversioned, regex):
 files = find_matching_files(directory, regex)
 get_numbers = re.compile('(\\d+).(\\d+).(\\d+)')
 def max2(a, b):
  'Returns the larger of the two values'
  av = get_numbers.search(os.path.basename(a)).groups()
  bv = get_numbers.search(os.path.basename(b)).groups()
  ret = (cmp(av[0], bv[0]) or cmp(av[1], bv[1]) or cmp(av[2], bv[2]))
  if (ret < 0):
   return b
  return a
 if (len(files) > 0):
  return reduce(max2, files)
 last_chance = os.path.join(directory, unversioned)
 if os.path.exists(last_chance):
  return last_chance
 return None

def find_highest_files ( directory , unversioned , regex ) : files = find_matching_files ( directory , regex ) get_numbers = re . compile ( '(\\d+).(\\d+).(\\d+)' ) def max2 ( a , b ) : av = get_numbers . search ( os . path . basename ( a ) ) . groups ( ) bv = get_numbers . search ( os . path . basename ( b ) ) . groups ( ) ret = ( cmp ( av [ 0 ] , bv [ 0 ] ) or cmp ( av [ 1 ] , bv [ 1 ] ) or cmp ( av [ 2 ] , bv [ 2 ] ) ) if ( ret < 0 ) : return b return a if ( len ( files ) > 0 ) : return reduce ( max2 , files ) last_chance = os . path . join ( directory , unversioned ) if os . path . exists ( last_chance ) : return last_chance return None
find the highest numbered file in a given directory that matches a given pattern .