From c4b6edf3b8b647e7ef43f1ba49a310fb93468919 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 6 Aug 2010 16:31:44 +0000 Subject: [PATCH] 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 --- django/contrib/admin/options.py | 2 +- django/contrib/admin/sites.py | 6 +++--- django/contrib/databrowse/plugins/calendars.py | 2 +- django/contrib/databrowse/plugins/fieldchoices.py | 2 +- django/forms/forms.py | 2 +- django/forms/formsets.py | 14 ++++++-------- django/utils/translation/trans_real.py | 2 +- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 1f8ff6dbd18..d44a5121f81 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -501,7 +501,7 @@ class ModelAdmin(BaseModelAdmin): # Convert the actions into a SortedDict keyed by name # and sorted by description. - actions.sort(lambda a,b: cmp(a[2].lower(), b[2].lower())) + actions.sort(key=lambda k: k[2].lower()) actions = SortedDict([ (name, (func, name, desc)) for func, name, desc in actions diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index 44464904510..d0d6473a34f 100644 --- a/django/contrib/admin/sites.py +++ b/django/contrib/admin/sites.py @@ -379,11 +379,11 @@ class AdminSite(object): # Sort the apps alphabetically. app_list = app_dict.values() - app_list.sort(lambda x, y: cmp(x['name'], y['name'])) + app_list.sort(key=lambda x: x['name']) # Sort the models alphabetically within each app. for app in app_list: - app['models'].sort(lambda x, y: cmp(x['name'], y['name'])) + app['models'].sort(key=lambda x: x['name']) context = { 'title': _('Site administration'), @@ -443,7 +443,7 @@ class AdminSite(object): if not app_dict: raise http.Http404('The requested admin page does not exist.') # Sort the models alphabetically within each app. - app_dict['models'].sort(lambda x, y: cmp(x['name'], y['name'])) + app_dict['models'].sort(key=lambda x: x['name']) context = { 'title': _('%s administration') % capfirst(app_label), 'app_list': [app_dict], diff --git a/django/contrib/databrowse/plugins/calendars.py b/django/contrib/databrowse/plugins/calendars.py index 9bbd02da26c..8a08cfd8eb9 100644 --- a/django/contrib/databrowse/plugins/calendars.py +++ b/django/contrib/databrowse/plugins/calendars.py @@ -60,7 +60,7 @@ class CalendarPlugin(DatabrowsePlugin): def homepage_view(self, request): easy_model = EasyModel(self.site, self.model) field_list = self.fields.values() - field_list.sort(lambda x, y: cmp(x.verbose_name, y.verbose_name)) + field_list.sort(key=lambda k:k.verbose_name) return render_to_response('databrowse/calendar_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list}) def calendar_view(self, request, field, year=None, month=None, day=None): diff --git a/django/contrib/databrowse/plugins/fieldchoices.py b/django/contrib/databrowse/plugins/fieldchoices.py index 8f77792579d..e0c01e95e63 100644 --- a/django/contrib/databrowse/plugins/fieldchoices.py +++ b/django/contrib/databrowse/plugins/fieldchoices.py @@ -61,7 +61,7 @@ class FieldChoicePlugin(DatabrowsePlugin): def homepage_view(self, request): easy_model = EasyModel(self.site, self.model) field_list = self.fields.values() - field_list.sort(lambda x, y: cmp(x.verbose_name, y.verbose_name)) + field_list.sort(key=lambda k: k.verbose_name) return render_to_response('databrowse/fieldchoice_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list}) def field_view(self, request, field, value=None): diff --git a/django/forms/forms.py b/django/forms/forms.py index b3718efa9a6..1a20b3b82a3 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -35,7 +35,7 @@ def get_declared_fields(bases, attrs, with_base_fields=True): Also integrates any additional media definitions """ fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] - fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) + fields.sort(key=lambda x: x[1].creation_counter) # If this class is subclassing another Form, add that Form's fields. # Note that we loop over the bases in *reverse*. This is necessary in diff --git a/django/forms/formsets.py b/django/forms/formsets.py index 3804f2b3c11..508950eee98 100644 --- a/django/forms/formsets.py +++ b/django/forms/formsets.py @@ -199,14 +199,12 @@ class BaseFormSet(StrAndUnicode): # A sort function to order things numerically ascending, but # None should be sorted below anything else. Allowing None as # a comparison value makes it so we can leave ordering fields - # blamk. - def compare_ordering_values(x, y): - if x[1] is None: - return 1 - if y[1] is None: - return -1 - return x[1] - y[1] - self._ordering.sort(compare_ordering_values) + # blank. + def compare_ordering_key(k): + if k[1] is None: + return (1, 0) # +infinity, larger than any number + return (0, k[1]) + self._ordering.sort(key=compare_ordering_key) # Return a list of form.cleaned_data dicts in the order spcified by # the form data. return [self.forms[i[0]] for i in self._ordering] diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index b528f8e586d..98b8d940b34 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -496,7 +496,7 @@ def parse_accept_lang_header(lang_string): return [] priority = priority and float(priority) or 1.0 result.append((lang, priority)) - result.sort(lambda x, y: -cmp(x[1], y[1])) + result.sort(key=lambda k: k[1], reverse=True) return result # get_date_formats and get_partial_date_formats aren't used anymore by Django