[Commits] (alecf) simplify and speed up sort-by-depth by using the key= parameter to sort... thank you Python Cookbook!

commits at osafoundation.org commits at osafoundation.org
Thu Apr 14 18:35:51 PDT 2005


Commit by: alecf
Modified files:
chandler/parcels/osaf/framework/blocks/calendar/CalendarCanvas.py 1.66 1.67

Log message:
simplify and speed up sort-by-depth by using the key= parameter to sort... thank you Python Cookbook!

ViewCVS links:
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/framework/blocks/calendar/CalendarCanvas.py.diff?r1=text&tr1=1.66&r2=text&tr2=1.67

Index: chandler/parcels/osaf/framework/blocks/calendar/CalendarCanvas.py
diff -u chandler/parcels/osaf/framework/blocks/calendar/CalendarCanvas.py:1.66 chandler/parcels/osaf/framework/blocks/calendar/CalendarCanvas.py:1.67
--- chandler/parcels/osaf/framework/blocks/calendar/CalendarCanvas.py:1.66	Thu Apr 14 16:47:16 2005
+++ chandler/parcels/osaf/framework/blocks/calendar/CalendarCanvas.py	Thu Apr 14 18:35:50 2005
@@ -1,8 +1,8 @@
 """ Canvas for calendaring blocks
 """
 
-__version__ = "$Revision: 1.66 $"
-__date__ = "$Date: 2005/04/14 23:47:16 $"
+__version__ = "$Revision: 1.67 $"
+__date__ = "$Date: 2005/04/15 01:35:50 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__ = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -1212,7 +1212,8 @@
             for rect in rects:
                 dc.DrawRectangleRect(rect)
 
-    def sortByStartTime(self, item1, item2):
+    @staticmethod
+    def sortByStartTime(item1, item2):
         """
         Comparison function for sorting, mostly by start time
         """
@@ -1224,18 +1225,6 @@
             dateResult = DateTime.cmp(item2.endTime, item1.endTime)
         return dateResult
         
-    def sortByDepth(self, canvasItem1, canvasItem2):
-        """
-        Comparison by depth - sorts events by how "deep" they will appear
-        on the canvas
-        """
-        diff = canvasItem1.GetIndentLevel() - canvasItem2.GetIndentLevel()
-        if diff:
-            return diff
-        return self.sortByStartTime(canvasItem1.GetItem(), \
-                                    canvasItem2.GetItem())
-        
-        
     def DrawCells(self, dc):
         styles = self.parent
         self._doDrawingCalculations()
@@ -1269,7 +1258,8 @@
         # canvasItemList has to be sorted by depth
         # should be relatively quick because the canvasItemList is already
         # sorted by startTime. If no conflicts, this is an O(n) operation
-        self.canvasItemList.sort(self.sortByDepth)
+        # (note that as of Python 2.4, sorts are stable, so this remains safe)
+        self.canvasItemList.sort(key=ColumnarCanvasItem.GetIndentLevel)
         
         selectedBox = None        
         # finally, draw the items



More information about the Commits mailing list