Use list.sort's keyword parameters
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 20 May 2007 16:29:56 +0000 (18:29 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 20 May 2007 16:29:56 +0000 (18:29 +0200)
src/packages/kofoto/commandline/main.py
src/packages/kofoto/gkofoto/categories.py
src/packages/kofoto/gkofoto/generatehtmldialog.py
src/packages/kofoto/gkofoto/objectcollectionview.py
src/packages/kofoto/gkofoto/objectselection.py
src/test/dagtests.py
src/test/searchtests.py
src/test/shelftests.py

index dfeff57..b44d965 100755 (executable)
@@ -1066,8 +1066,7 @@ def cmdSortAlbum(env, args):
     else:
         attr = u"captured"
     album = sloppyGetAlbum(env, args[0])
-    children = list(album.getChildren())
-    children.sort(key=lambda x: x.getAttribute(attr))
+    children = sorted(album.getChildren(), key=lambda x: x.getAttribute(attr))
     album.setChildren(children)
 
 
@@ -1314,8 +1313,8 @@ def main(argv):
         defaultlimit = env.config.getcoordlist(
             "album generation", "default_image_size_limit")[0]
         imgsizesset.add(defaultlimit)
-        imgsizes = list(imgsizesset)
-        imgsizes.sort(lambda x, y: cmp(x[0] * x[1], y[0] * y[1]))
+        imgsizes = sorted(
+            imgsizesset, key=lambda x, y: cmp(x[0] * x[1], y[0] * y[1]))
         env.imagesizelimits = imgsizes
 
         commandTable[args[0]](env, args[1:])
index b19607e..e1a0db8 100644 (file)
@@ -413,8 +413,9 @@ class Categories:
             return
 
         regexp = re.compile(".*%s.*" % re.escape(text.lower()))
-        categories = list(env.shelf.getMatchingCategories(regexp))
-        categories.sort(self.__compareCategories)
+        categories = sorted(
+            env.shelf.getMatchingCategories(regexp),
+            cmp=self.__compareCategories)
         exactMatches = []
         for category in categories:
             iterator = self.__categoryQSModel.append()
@@ -687,8 +688,7 @@ class Categories:
             categoryRow[self.__COLUMN_DESCRIPTION] = category.getDescription()
 
     def __sortCategories(self, categoryIter):
-        categories = list(categoryIter)
-        categories.sort(self.__compareCategories)
+        categories = sorted(categoryIter, cmp=self.__compareCategories)
         return categories
 
     def __compareCategories(self, x, y):
index 89e75ff..99f1d3b 100644 (file)
@@ -82,8 +82,8 @@ class GenerateHTMLDialog:
         defaultlimit = env.config.getcoordlist(
             "album generation", "default_image_size_limit")[0]
         imgsizesset.add(defaultlimit)
-        imgsizes = list(imgsizesset)
-        imgsizes.sort(lambda x, y: cmp(x[0] * x[1], y[0] * y[1]))
+        imgsizes = sorted(
+            imgsizesset, key=lambda x, y: cmp(x[0] * x[1], y[0] * y[1]))
         env.imagesizelimits = imgsizes
 
         try:
index ea21fdc..229254d 100644 (file)
@@ -361,8 +361,7 @@ class ObjectCollectionView:
                                        gtk.SORT_DESCENDING)
             menuGroup.addSeparator()
             objectMetadataMap = objectCollection.getObjectMetadataMap()
-            columnNames = list(objectMetadataMap.keys())
-            columnNames.sort()
+            columnNames = sorted(objectMetadataMap)
             for columnName in columnNames:
                 if objectMetadataMap[columnName][ObjectCollection.TYPE] != gtk.gdk.Pixbuf:
                     menuGroup.addRadioMenuItem(columnName,
index 329bbba..26ba2f4 100644 (file)
@@ -53,9 +53,8 @@ class ObjectSelection:
         return [x[1] for x in items]
 
     def getLowestSelectedRowNr(self):
-        rowNrs = list(self)
+        rowNrs = sorted(self)
         if (len(rowNrs) > 0):
-            rowNrs.sort()
             return rowNrs[0]
         else:
             return None
@@ -69,8 +68,7 @@ class ObjectSelection:
         if len(self) == 0:
             rowNr = 0
         else:
-            rowNumbers = list(self)
-            rowNumbers.sort()
+            rowNumbers = sorted(self)
             rowNr = rowNumbers[0]
         filenames = []
         for x in [rowNr - 2, rowNr + 2, rowNr - 1, rowNr + 1]: # TODO: Make configurable.
index c8f92f8..e92730e 100755 (executable)
@@ -15,11 +15,6 @@ from kofoto.dag import *
 PICDIR = unicode(os.path.realpath(
     os.path.join("..", "reference_pictures", "working")))
 
-def sorted(x):
-    y = x[:]
-    y.sort()
-    return y
-
 class TestDAG(unittest.TestCase):
     def setUp(self):
         self.dag = DAG()
index b787d36..6c0f29a 100755 (executable)
@@ -75,8 +75,8 @@ class TestSearch(TestShelfFixture):
         parser = Parser(self.shelf)
         for expression, expectedResult in tests:
             parseTree = parser.parse(expression)
-            result = list(self.shelf.search(parseTree))
-            result.sort(key=lambda x: x.getId())
+            result = sorted(
+                self.shelf.search(parseTree), key=lambda x: x.getId())
             assert result == expectedResult, (expression, expectedResult, result)
 
     def test_parseErrors(self):
index 1fe5d5c..4c77fa5 100755 (executable)
@@ -555,8 +555,7 @@ class TestShelfMethods(TestShelfFixture):
             assert False
 
     def test_getAllAttributeNames(self):
-        attrnames = list(self.shelf.getAllAttributeNames())
-        attrnames.sort()
+        attrnames = sorted(self.shelf.getAllAttributeNames())
         assert attrnames == [
             "cameramake", "cameramodel", "captured", "description",
             "digitalzoom", "exposurebias", "exposureprogram", "exposuretime",
@@ -593,12 +592,12 @@ class TestShelfMethods(TestShelfFixture):
             assert False
 
     def test_getRootCategories(self):
-        categories = list(self.shelf.getRootCategories())
+        categories = sorted(
+            self.shelf.getRootCategories(), key=lambda x: x.getTag())
         cat_a = self.shelf.getCategoryByTag(u"a")
         cat_events = self.shelf.getCategoryByTag(u"events")
         cat_locations = self.shelf.getCategoryByTag(u"locations")
         cat_people = self.shelf.getCategoryByTag(u"people")
-        categories.sort(key=lambda x: x.getTag())
         assert categories == [cat_a, cat_events, cat_locations, cat_people], \
                categories
 
@@ -617,8 +616,7 @@ class TestCategory(TestShelfFixture):
         cat_a.setDescription(u"foo")
         assert cat_a.getDescription() == "foo"
 
-        a_children = list(cat_a.getChildren())
-        a_children.sort(key=lambda x: x.getId())
+        a_children = sorted(cat_a.getChildren(), key=lambda x: x.getId())
         assert a_children == [cat_b, cat_c]
         b_children = list(cat_b.getChildren())
         assert b_children == [cat_d]
@@ -629,8 +627,7 @@ class TestCategory(TestShelfFixture):
         assert a_parents == []
         b_parents = list(cat_b.getParents())
         assert b_parents == [cat_a]
-        d_parents = list(cat_d.getParents())
-        d_parents.sort(key=lambda x: x.getTag())
+        d_parents = sorted(cat_d.getParents(), key=lambda x: x.getTag())
         assert d_parents == [cat_b, cat_c]
 
         assert not cat_a.isChildOf(cat_a)
@@ -683,8 +680,7 @@ class TestObject(TestShelfFixture):
         root = self.shelf.getRootAlbum()
         alpha = self.shelf.getAlbumByTag(u"alpha")
         beta = self.shelf.getAlbumByTag(u"beta")
-        parents = list(beta.getParents())
-        parents.sort(key=lambda x: x.getTag())
+        parents = sorted(beta.getParents(), key=lambda x: x.getTag())
         assert parents == [alpha, root]
 
     def test_getAttribute(self):
@@ -703,8 +699,7 @@ class TestObject(TestShelfFixture):
 
     def test_getAttributeNames(self):
         orphans = self.shelf.getAlbumByTag(u"orphans")
-        names = list(orphans.getAttributeNames())
-        names.sort()
+        names = sorted(orphans.getAttributeNames())
         assert names == ["description", "title"]
 
     def test_setAttribute(self):
@@ -781,8 +776,7 @@ class TestAlbum(TestShelfFixture):
     def test_getAlbumParents(self):
         root = self.shelf.getRootAlbum()
         alpha = self.shelf.getAlbumByTag(u"alpha")
-        parents = list(alpha.getAlbumParents())
-        parents.sort(key=lambda x: x.getTag())
+        parents = sorted(alpha.getAlbumParents(), key=lambda x: x.getTag())
         assert parents == [root]
 
     def test_isAlbum(self):