Commits

Iñigo Serna committed abef58c

Clean and optimize files sorting, use "key" argument in sorted.

* lfm/files.py (sort_dir): clean and optimize code
* lfm/files.py (__do_sort): use python v2.4+ "key" arg in sorted

Comments (0)

Files changed (3)

+Sat May 14 17:46:18 2011  Iñigo Serna  <inigoserna@gmail.com>
+
+	* lfm/files.py (sort_dir): clean and optimize code
+	* lfm/files.py (__do_sort): use python v2.4+ "key" arg in sorted
+
 Sat May 14 12:15:47 2011  Iñigo Serna  <inigoserna@gmail.com>
 
 	* lfm/lfm.py (PowerCLI.__check_loop): update code to use python v2.5+ "any"
 ============================================================================
-Last update:	Time-stamp: <2011-05-14 12:17:47 inigo>
+Last update:	Time-stamp: <2011-05-14 17:42:27 inigo>
 ============================================================================
 
 
 ========
 High Priority:
   + enhancements:
-    - clean code
-      . look to use key in sort/ed cmp
     - test and improve PowerCLI
     - don't like much how recursive chmod/chown/chgrp currently works
       . split in 2 different features chmod and chown/chgrp ?
 ########################################################################
 ##### sort
 def __do_sort(f_dict, sortmode, sort_mix_cases):
-    def __move_pardir_to_top(names):
-        if names.count(os.pardir) != 0:
-            names.remove(os.pardir)
-            names.insert(0, os.pardir)
-        return names
-
     if sortmode == SORTTYPE_None:
-        return __move_pardir_to_top(f_dict.keys())
-
-    if sortmode in (SORTTYPE_byName, SORTTYPE_byName_rev):
-        if sort_mix_cases:
-            mycmp = lambda a, b: cmp(a.lower(), b.lower())
-        else:
-            mycmp = None
         names = f_dict.keys()
-        names.sort(cmp=mycmp)
-        if sortmode == SORTTYPE_byName_rev:
-            names.reverse()
-        return __move_pardir_to_top(names)
-
-    mydict = {}
-    for k in f_dict.keys():
-        if sortmode in (SORTTYPE_bySize, SORTTYPE_bySize_rev):
-            size = f_dict[k][FT_SIZE]
-            while mydict.has_key(size):    # can't be 2 entries with same key
-                size += 0.1
-            mydict[size] = k
-        elif sortmode in (SORTTYPE_byDate, SORTTYPE_byDate_rev):
-            tim = f_dict[k][FT_MTIME]
-            while mydict.has_key(tim):    # can't be 2 entries with same key
-                tim += 0.1
-            mydict[tim] = k
-        else:
-            raise ValueError
-    values = mydict.keys()
-    values.sort()
-    names = [mydict[v] for v in values]
-    if sortmode in (SORTTYPE_bySize_rev, SORTTYPE_byDate_rev):
-        names.reverse()
-    return __move_pardir_to_top(names)
+    elif sortmode in (SORTTYPE_byName, SORTTYPE_byName_rev):
+        names = sorted(f_dict.keys(),
+                       key=lambda f: f.lower() if sort_mix_cases else f,
+                       reverse=sortmode==SORTTYPE_byName_rev)
+    elif sortmode in (SORTTYPE_bySize, SORTTYPE_bySize_rev):
+        names = sorted(f_dict.keys(),
+                       key=lambda f: f_dict[f][FT_SIZE],
+                       reverse=sortmode==SORTTYPE_bySize_rev)
+    elif sortmode in (SORTTYPE_byDate, SORTTYPE_byDate_rev):
+        names = sorted(f_dict.keys(),
+                       key=lambda f: f_dict[f][FT_MTIME],
+                       reverse=sortmode==SORTTYPE_byDate_rev)
+    if names.count(os.pardir) != 0: # move pardir to top
+        names.remove(os.pardir)
+        names.insert(0, os.pardir)
+    return names
 
 
 def sort_dir(files_dict, sortmode, sort_mix_dirs, sort_mix_cases):
     """return an array of files which are sorted by mode"""
 
-    # separate directories and files
     d, f = {}, {}
     if sort_mix_dirs:
         f = files_dict
+        d1 = []
     else:
         for k, v in files_dict.items():
             if v[FT_TYPE] in (FTYPE_DIR, FTYPE_LNK2DIR):
                 d[k] = v
             else:
                 f[k] = v
-    # sort
-    if d:
         d1 = __do_sort(d, sortmode, sort_mix_cases)
-    else:
-        d1 = []
     d2 = __do_sort(f, sortmode, sort_mix_cases)
     d1.extend(d2)
     return d1