Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12746 -- Updated sorting calls to use 'key' instead of 'cmp'. …

…This will be slightly faster in certain circumstances, but more importantly, is a required step for migration to Python 3. Thanks to Martin van Loewis for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13509 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c4b6edf3b8b647e7ef43f1ba49a310fb93468919 1 parent 9e5899f
Russell Keith-Magee authored August 06, 2010
2  django/contrib/admin/options.py
@@ -501,7 +501,7 @@ def get_actions(self, request):
501 501
 
502 502
         # Convert the actions into a SortedDict keyed by name
503 503
         # and sorted by description.
504  
-        actions.sort(lambda a,b: cmp(a[2].lower(), b[2].lower()))
  504
+        actions.sort(key=lambda k: k[2].lower())
505 505
         actions = SortedDict([
506 506
             (name, (func, name, desc))
507 507
             for func, name, desc in actions
6  django/contrib/admin/sites.py
@@ -379,11 +379,11 @@ def index(self, request, extra_context=None):
379 379
 
380 380
         # Sort the apps alphabetically.
381 381
         app_list = app_dict.values()
382  
-        app_list.sort(lambda x, y: cmp(x['name'], y['name']))
  382
+        app_list.sort(key=lambda x: x['name'])
383 383
 
384 384
         # Sort the models alphabetically within each app.
385 385
         for app in app_list:
386  
-            app['models'].sort(lambda x, y: cmp(x['name'], y['name']))
  386
+            app['models'].sort(key=lambda x: x['name'])
387 387
 
388 388
         context = {
389 389
             'title': _('Site administration'),
@@ -443,7 +443,7 @@ def app_index(self, request, app_label, extra_context=None):
443 443
         if not app_dict:
444 444
             raise http.Http404('The requested admin page does not exist.')
445 445
         # Sort the models alphabetically within each app.
446  
-        app_dict['models'].sort(lambda x, y: cmp(x['name'], y['name']))
  446
+        app_dict['models'].sort(key=lambda x: x['name'])
447 447
         context = {
448 448
             'title': _('%s administration') % capfirst(app_label),
449 449
             'app_list': [app_dict],
2  django/contrib/databrowse/plugins/calendars.py
@@ -60,7 +60,7 @@ def model_view(self, request, model_databrowse, url):
60 60
     def homepage_view(self, request):
61 61
         easy_model = EasyModel(self.site, self.model)
62 62
         field_list = self.fields.values()
63  
-        field_list.sort(lambda x, y: cmp(x.verbose_name, y.verbose_name))
  63
+        field_list.sort(key=lambda k:k.verbose_name)
64 64
         return render_to_response('databrowse/calendar_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list})
65 65
 
66 66
     def calendar_view(self, request, field, year=None, month=None, day=None):
2  django/contrib/databrowse/plugins/fieldchoices.py
@@ -61,7 +61,7 @@ def model_view(self, request, model_databrowse, url):
61 61
     def homepage_view(self, request):
62 62
         easy_model = EasyModel(self.site, self.model)
63 63
         field_list = self.fields.values()
64  
-        field_list.sort(lambda x, y: cmp(x.verbose_name, y.verbose_name))
  64
+        field_list.sort(key=lambda k: k.verbose_name)
65 65
         return render_to_response('databrowse/fieldchoice_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list})
66 66
 
67 67
     def field_view(self, request, field, value=None):
2  django/forms/forms.py
@@ -35,7 +35,7 @@ def get_declared_fields(bases, attrs, with_base_fields=True):
35 35
     Also integrates any additional media definitions
36 36
     """
37 37
     fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
38  
-    fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
  38
+    fields.sort(key=lambda x: x[1].creation_counter)
39 39
 
40 40
     # If this class is subclassing another Form, add that Form's fields.
41 41
     # Note that we loop over the bases in *reverse*. This is necessary in
14  django/forms/formsets.py
@@ -199,14 +199,12 @@ def _get_ordered_forms(self):
199 199
             # A sort function to order things numerically ascending, but
200 200
             # None should be sorted below anything else. Allowing None as
201 201
             # a comparison value makes it so we can leave ordering fields
202  
-            # blamk.
203  
-            def compare_ordering_values(x, y):
204  
-                if x[1] is None:
205  
-                    return 1
206  
-                if y[1] is None:
207  
-                    return -1
208  
-                return x[1] - y[1]
209  
-            self._ordering.sort(compare_ordering_values)
  202
+            # blank.
  203
+            def compare_ordering_key(k):
  204
+                if k[1] is None:
  205
+                    return (1, 0) # +infinity, larger than any number
  206
+                return (0, k[1])
  207
+            self._ordering.sort(key=compare_ordering_key)
210 208
         # Return a list of form.cleaned_data dicts in the order spcified by
211 209
         # the form data.
212 210
         return [self.forms[i[0]] for i in self._ordering]
2  django/utils/translation/trans_real.py
@@ -496,7 +496,7 @@ def parse_accept_lang_header(lang_string):
496 496
             return []
497 497
         priority = priority and float(priority) or 1.0
498 498
         result.append((lang, priority))
499  
-    result.sort(lambda x, y: -cmp(x[1], y[1]))
  499
+    result.sort(key=lambda k: k[1], reverse=True)
500 500
     return result
501 501
 
502 502
 # get_date_formats and get_partial_date_formats aren't used anymore by Django

0 notes on commit c4b6edf

Please sign in to comment.
Something went wrong with that request. Please try again.