From 439e26dab14d4811f96f9ae3407a8b88fb78a64d Mon Sep 17 00:00:00 2001
From: Kazuhiko Shiozaki <kazuhiko@nexedi.com>
Date: Tue, 23 Sep 2008 12:32:40 +0000
Subject: [PATCH] use sort(key=) instead of sort(cmp=) for better performance.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@23760 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/AmortisationRule.py | 2 +-
 product/ERP5/Document/BusinessTemplate.py | 8 +++-----
 product/ERP5/Document/Container.py        | 6 +++---
 product/ERP5/Document/Image.py            | 5 ++++-
 product/ERP5/Document/MovementGroup.py    | 2 +-
 product/ERP5/Document/OrderBuilder.py     | 3 +--
 product/ERP5/Document/WebSite.py          | 2 +-
 product/ERP5/tests/testImmobilisation.py  | 8 ++++----
 8 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/product/ERP5/Document/AmortisationRule.py b/product/ERP5/Document/AmortisationRule.py
index 60d31ce0e6..76470a1c87 100644
--- a/product/ERP5/Document/AmortisationRule.py
+++ b/product/ERP5/Document/AmortisationRule.py
@@ -368,7 +368,7 @@ class AmortisationRule(Rule):
       for current_dict in (aggregated_period_dict, calculated_period_dict):
         for type_dict in current_dict.values():
           for movement_list in type_dict.values():
-            movement_list.sort( lambda a,b: cmp(a['stop_date'], b['stop_date']) )
+            movement_list.sort(key=lambda x: x['stop_date'])
       matched_dict = self._matchAmortisationPeriods(calculated_period_dict, aggregated_period_dict)
       
       # We can now apply the calculated movements on the applied rule
diff --git a/product/ERP5/Document/BusinessTemplate.py b/product/ERP5/Document/BusinessTemplate.py
index 0d01a004e9..53a5a1a95e 100644
--- a/product/ERP5/Document/BusinessTemplate.py
+++ b/product/ERP5/Document/BusinessTemplate.py
@@ -1256,11 +1256,9 @@ class SkinTemplateItem(ObjectTemplateItem):
             new_selection.append(skin_id)
       new_selection.extend(selection)
       # sort the layer according to skin priorities
-      new_selection.sort(lambda a, b : cmp(
-        b in ps.objectIds() and ps[b].getProperty(
-            'business_template_skin_layer_priority', 0) or 0,
-        a in ps.objectIds() and ps[a].getProperty(
-            'business_template_skin_layer_priority', 0) or 0))
+      new_selection.sort(
+        key=lambda x: x in ps.objectIds() and -ps[x].getProperty(
+        'business_template_skin_layer_priority', 0) or 0)
       ps.manage_skinLayers(skinpath = tuple(new_selection), skinname = skin_name, add_skin = 1)
     # Make sure that skin data is up-to-date (see CMFCore/Skinnable.py).
     p.changeSkin(None)
diff --git a/product/ERP5/Document/Container.py b/product/ERP5/Document/Container.py
index 1d38194b3e..d062a13652 100644
--- a/product/ERP5/Document/Container.py
+++ b/product/ERP5/Document/Container.py
@@ -125,11 +125,11 @@ class Container(Movement, XMLObject):
       """
       result = ""
       container_line_list = list(self.objectValues())
-      container_line_list.sort(lambda x, y: cmp(x.getResource(), y.getResource()))
+      container_line_list.sort(key=lambda x: x.getResource())
       for container_line in container_line_list:
         if container_line.hasCellContent():
           container_cell_list = list(container_line.objectValues())
-          container_cell_list.sort(lambda x, y: cmp(x.getVariationText(), y.getVariationText()))
+          container_cell_list.sort(key=lambda x: x.getVariationText())
           for container_cell in container_cell_list:
             result += "%s %s %s\n" % (container_cell.getResource(), 
                                       container_cell.getQuantity(), 
@@ -137,7 +137,7 @@ class Container(Movement, XMLObject):
         else:
           result += "%s %s\n" % (container_line.getResource(), container_line.getQuantity())
       container_list = list(self.objectValues(spec = self.meta_type))
-      container_list.sort(lambda x, y: cmp(x.getContainerText(), y.getContainerText()))
+      container_list.sort(key=lambda x: x.getContainerText())
       more_result = ""
       for container in container_list:
         more_result += container.getContainerText()
diff --git a/product/ERP5/Document/Image.py b/product/ERP5/Document/Image.py
index f261353861..5c7e154ee4 100644
--- a/product/ERP5/Document/Image.py
+++ b/product/ERP5/Document/Image.py
@@ -266,7 +266,10 @@ class Image(File, OFSImage):
           if id in id_list:
             id_list.remove(id)
       # Sort by desired photo surface area
-      id_list.sort(lambda x,y,d=self.getSizeFromImageDisplay: cmp(d(x)[0]*d(x)[1], d(y)[0]*d(y)[1]))
+      def getSurfaceArea(img):
+        x, y = self.getSizeFromImageDisplay(img)
+        return x * y
+      id_list.sort(key=getSurfaceArea)
       return id_list
 
   security.declareProtected('Access contents information', 'displayLinks')
diff --git a/product/ERP5/Document/MovementGroup.py b/product/ERP5/Document/MovementGroup.py
index 7369ef3d4a..db6fabe0ef 100644
--- a/product/ERP5/Document/MovementGroup.py
+++ b/product/ERP5/Document/MovementGroup.py
@@ -76,6 +76,6 @@ class MovementGroup(XMLObject):
   def separate(self, movement_list):
     # We sort group of simulation movements by their IDs.
     # DO NOT OVERRIDE THIS METHOD. Override _separate() instead.
-    return sorted([[sorted(x[0], lambda a,b:cmp(a.getId(), b.getId())), x[1]] \
+    return sorted([[sorted(x[0], key=lambda x: x.getId()), x[1]] \
                    for x in self._separate(movement_list)],
                   lambda a,b: cmp(a[0][0].getId(), b[0][0].getId()))
diff --git a/product/ERP5/Document/OrderBuilder.py b/product/ERP5/Document/OrderBuilder.py
index 993f1ed2bb..2ade2a1f5a 100644
--- a/product/ERP5/Document/OrderBuilder.py
+++ b/product/ERP5/Document/OrderBuilder.py
@@ -263,8 +263,7 @@ class OrderBuilder(XMLObject, Amount, Predicate):
         original = None
       if original is not None:
         original_id = original.getId()
-        instance_list.sort(lambda a,b:cmp(b.getId()==original_id,
-                                          a.getId()==original_id))
+        instance_list.sort(key=lambda x: x.getId() != original_id and 1 or 0)
       for instance_to_update in instance_list:
         result, property_dict = self._test(
           instance_to_update, movement_group_list, divergence_list)
diff --git a/product/ERP5/Document/WebSite.py b/product/ERP5/Document/WebSite.py
index ea273dda2b..4dadc8385d 100644
--- a/product/ERP5/Document/WebSite.py
+++ b/product/ERP5/Document/WebSite.py
@@ -234,6 +234,6 @@ class WebSite(WebSection):
       section_list = section_dict.values()
 
       # Sort by Index
-      section_list.sort(lambda x,y: cmp(x.getIntIndex(), y.getIntIndex()))
+      section_list.sort(key=lambda x: x.getIntIndex())
 
       return section_list
diff --git a/product/ERP5/tests/testImmobilisation.py b/product/ERP5/tests/testImmobilisation.py
index 41f6a715a8..568cdc8f4e 100644
--- a/product/ERP5/tests/testImmobilisation.py
+++ b/product/ERP5/tests/testImmobilisation.py
@@ -2265,7 +2265,7 @@ class TestImmobilisationMixin(ERP5TypeTestCase):
     
     c_transaction_list = self.getPortal().portal_catalog(portal_type='Amortisation Transaction')
     c_transaction_list = [o.getObject() for o in c_transaction_list]
-    c_transaction_list.sort(lambda a,b: cmp(a.getStopDate(),b.getStopDate()))
+    c_transaction_list.sort(key=lambda x: x.getStopDate())
     self._testAccountingBuild(c_transaction_list, e_transaction_list)
   
     
@@ -2298,7 +2298,7 @@ class TestImmobilisationMixin(ERP5TypeTestCase):
     
     c_transaction_list = self.getPortal().portal_catalog(portal_type='Amortisation Transaction')
     c_transaction_list = [o.getObject() for o in c_transaction_list]
-    #c_transaction_list.sort(lambda a,b: cmp(a.getStopDate(),b.getStopDate()))
+    #c_transaction_list.sort(key=lambda x: x.getStopDate())
     self._testAccountingBuild(c_transaction_list, e_transaction_list)
   
     
@@ -2994,9 +2994,9 @@ class TestImmobilisationMixin(ERP5TypeTestCase):
           except:
             pass
           if type(c_value) == type([]):
-            c_value.sort(lambda a,b: cmp(a.getId(), b.getId()))
+            c_value.sort(key=lambda x: x.getId())
           if type(e_value) == type([]):
-            e_value.sort(lambda a,b: cmp(a.getId(), b.getId()))
+            e_value.sort(key=lambda x: x.getId())
           if is_float:
             wrong_transaction = (round(c_value,2) != round(e_value,2))
           else:
-- 
2.30.9