Use list.sort's new keyword parameters
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 20 May 2007 15:54:35 +0000 (17:54 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 20 May 2007 15:54:35 +0000 (17:54 +0200)
src/packages/kofoto/commandline/main.py
src/packages/kofoto/gkofoto/albummembers.py
src/packages/kofoto/gkofoto/objectcollection.py
src/packages/kofoto/gkofoto/objectselection.py
src/packages/kofoto/gkofoto/tableview.py
src/packages/kofoto/outputengine.py
src/test/searchtests.py
src/test/shelftests.py

index 3a57509..dfeff57 100755 (executable)
@@ -938,8 +938,7 @@ def cmdRemove(env, args):
             positions.append(int(pos))
         except ValueError:
             env.errexit("Bad position: %s.\n" % pos)
-    positions.sort()
-    positions.reverse()
+    positions.sort(reverse=True)
     children = list(album.getChildren())
     if not (0 <= positions[0] < len(children)):
         env.errexit("Bad position: %d.\n" % positions[0])
@@ -1068,8 +1067,7 @@ def cmdSortAlbum(env, args):
         attr = u"captured"
     album = sloppyGetAlbum(env, args[0])
     children = list(album.getChildren())
-    children.sort(
-        lambda x, y: cmp(x.getAttribute(attr), y.getAttribute(attr)))
+    children.sort(key=lambda x: x.getAttribute(attr))
     album.setChildren(children)
 
 
index b8c9592..dd7e115 100644 (file)
@@ -65,11 +65,8 @@ class AlbumMembers(ObjectCollection):
         model = self.getModel()
         self._freezeViews()
         albumMembers = list(self.__album.getChildren())
-        locations = list(self.getObjectSelection())
-        locations.sort()
-        locations.reverse()
         albumDeleted = False
-        for loc in locations:
+        for loc in sorted(self.getObjectSelection(), reverse=True):
             if albumMembers[loc].isAlbum():
                 albumDeleted = True
             albumMembers.pop(loc)
index 42b1807..2ba7646 100644 (file)
@@ -238,9 +238,7 @@ class ObjectCollection(object):
             unsortedModel = self.getUnsortedModel()
             locations = [row.path for row in unsortedModel
                          if row[ObjectCollection.COLUMN_OBJECT_ID] in objectIds]
-            locations.sort()
-            locations.reverse()
-            for loc in locations:
+            for loc in sorted(locations, reverse=True):
                 del unsortedModel[loc]
         dialog.destroy()
         if albumDestroyed:
index dbcd76d..329bbba 100644 (file)
@@ -49,7 +49,7 @@ class ObjectSelection:
     def getSelectedObjects(self):
         conv = self.__objectCollection.convertFromUnsortedRowNr
         items = self.__selectedObjects.items()
-        items.sort(lambda x, y: cmp(conv(x[0]), conv(y[0])))
+        items.sort(key=lambda x: conv(x[0]))
         return [x[1] for x in items]
 
     def getLowestSelectedRowNr(self):
index 656cf47..c186aa6 100644 (file)
@@ -1,5 +1,6 @@
 import gtk
 import gobject
+import operator
 from kofoto.gkofoto.environment import env
 from kofoto.gkofoto.objectcollectionview import ObjectCollectionView
 from kofoto.gkofoto.objectcollection import ObjectCollection
@@ -84,7 +85,7 @@ class TableView(ObjectCollectionView):
         objectMetadataMap = self._objectCollection.getObjectMetadataMap()
         disabledFields = self._objectCollection.getDisabledFields()
         columnLocationList = self.__userChosenColumns.items()
-        columnLocationList.sort(lambda x, y: cmp(x[1], y[1]))
+        columnLocationList.sort(key=operator.itemgetter(1))
         env.debug("Column locations: " + str(columnLocationList))
         for (columnName, _) in columnLocationList:
             if (columnName in objectMetadataMap and
index bd7f299..0a54b59 100644 (file)
@@ -205,7 +205,7 @@ class OutputEngine:
         self.preGeneration(root)
         i = 1
         items = albummap.items()
-        items.sort(lambda x, y: cmp(x[0].getTag(), y[0].getTag()))
+        items.sort(key=lambda x: x[0].getTag())
         for album, paths in items:
             if album in albumsToGenerate:
                 nchildren = len(list(album.getChildren()))
index a1e6942..b787d36 100755 (executable)
@@ -76,7 +76,7 @@ class TestSearch(TestShelfFixture):
         for expression, expectedResult in tests:
             parseTree = parser.parse(expression)
             result = list(self.shelf.search(parseTree))
-            result.sort(lambda x, y: cmp(x.getId(), y.getId()))
+            result.sort(key=lambda x: x.getId())
             assert result == expectedResult, (expression, expectedResult, result)
 
     def test_parseErrors(self):
index d34d4fb..1fe5d5c 100755 (executable)
@@ -598,7 +598,7 @@ class TestShelfMethods(TestShelfFixture):
         cat_events = self.shelf.getCategoryByTag(u"events")
         cat_locations = self.shelf.getCategoryByTag(u"locations")
         cat_people = self.shelf.getCategoryByTag(u"people")
-        categories.sort(lambda x, y: cmp(x.getTag(), y.getTag()))
+        categories.sort(key=lambda x: x.getTag())
         assert categories == [cat_a, cat_events, cat_locations, cat_people], \
                categories
 
@@ -618,7 +618,7 @@ class TestCategory(TestShelfFixture):
         assert cat_a.getDescription() == "foo"
 
         a_children = list(cat_a.getChildren())
-        a_children.sort(lambda x, y: cmp(x.getId(), y.getId()))
+        a_children.sort(key=lambda x: x.getId())
         assert a_children == [cat_b, cat_c]
         b_children = list(cat_b.getChildren())
         assert b_children == [cat_d]
@@ -630,7 +630,7 @@ class TestCategory(TestShelfFixture):
         b_parents = list(cat_b.getParents())
         assert b_parents == [cat_a]
         d_parents = list(cat_d.getParents())
-        d_parents.sort(lambda x, y: cmp(x.getTag(), y.getTag()))
+        d_parents.sort(key=lambda x: x.getTag())
         assert d_parents == [cat_b, cat_c]
 
         assert not cat_a.isChildOf(cat_a)
@@ -684,7 +684,7 @@ class TestObject(TestShelfFixture):
         alpha = self.shelf.getAlbumByTag(u"alpha")
         beta = self.shelf.getAlbumByTag(u"beta")
         parents = list(beta.getParents())
-        parents.sort(lambda x, y: cmp(x.getTag(), y.getTag()))
+        parents.sort(key=lambda x: x.getTag())
         assert parents == [alpha, root]
 
     def test_getAttribute(self):
@@ -782,7 +782,7 @@ class TestAlbum(TestShelfFixture):
         root = self.shelf.getRootAlbum()
         alpha = self.shelf.getAlbumByTag(u"alpha")
         parents = list(alpha.getAlbumParents())
-        parents.sort(lambda x, y: cmp(x.getTag(), y.getTag()))
+        parents.sort(key=lambda x: x.getTag())
         assert parents == [root]
 
     def test_isAlbum(self):