Merge ~cjwatson/launchpad:py3-cmp into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 688851cb3b5ebb14c232712a95400bafe078aef7
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:py3-cmp
Merge into: launchpad:master
Diff against target: 406 lines (+64/-77)
10 files modified
lib/lp/archivepublisher/domination.py (+8/-4)
lib/lp/archivepublisher/tests/test_dominator.py (+10/-6)
lib/lp/archiveuploader/tests/nascentupload-announcements.txt (+1/-4)
lib/lp/bugs/doc/initial-bug-contacts.txt (+2/-4)
lib/lp/registry/browser/product.py (+5/-8)
lib/lp/registry/doc/distribution-mirror.txt (+2/-1)
lib/lp/registry/doc/teammembership-email-notification.txt (+3/-4)
lib/lp/registry/tests/test_distroseriesdifferencecomment.py (+2/-7)
lib/lp/services/doc/orderingcheck.txt (+1/-4)
lib/lp/services/webapp/sorting.py (+30/-35)
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+394483@code.launchpad.net

Commit message

Stop using cmp() and sorted(cmp=...)

Description of the change

These are no longer supported in Python 3; we need to use constructions involving sorted(key=...) instead.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/archivepublisher/domination.py b/lib/lp/archivepublisher/domination.py
2index 356a2ee..0e9a8b0 100644
3--- a/lib/lp/archivepublisher/domination.py
4+++ b/lib/lp/archivepublisher/domination.py
5@@ -54,6 +54,7 @@ __all__ = ['Dominator']
6
7 from collections import defaultdict
8 from datetime import timedelta
9+from functools import cmp_to_key
10 from operator import (
11 attrgetter,
12 itemgetter,
13@@ -198,14 +199,16 @@ class GeneralizedPublication:
14 self.getPackageVersion(pub1), self.getPackageVersion(pub2))
15
16 if version_comparison == 0:
17- # Use dates as tie breaker.
18- return cmp(pub1.datecreated, pub2.datecreated)
19+ # Use dates as tie breaker (idiom equivalent to Python 2's cmp).
20+ return (
21+ (pub1.datecreated > pub2.datecreated) -
22+ (pub1.datecreated < pub2.datecreated))
23 else:
24 return version_comparison
25
26 def sortPublications(self, publications):
27 """Sort publications from most to least current versions."""
28- return sorted(publications, cmp=self.compare, reverse=True)
29+ return sorted(publications, key=cmp_to_key(self.compare), reverse=True)
30
31
32 def find_live_source_versions(sorted_pubs):
33@@ -417,7 +420,8 @@ class Dominator:
34 len(sorted_pubs), live_versions)
35
36 # Verify that the publications are really sorted properly.
37- check_order = OrderingCheck(cmp=generalization.compare, reverse=True)
38+ check_order = OrderingCheck(
39+ key=cmp_to_key(generalization.compare), reverse=True)
40
41 current_dominant = None
42 dominant_version = None
43diff --git a/lib/lp/archivepublisher/tests/test_dominator.py b/lib/lp/archivepublisher/tests/test_dominator.py
44index 4d073e0..b35596e 100755
45--- a/lib/lp/archivepublisher/tests/test_dominator.py
46+++ b/lib/lp/archivepublisher/tests/test_dominator.py
47@@ -8,6 +8,7 @@ from __future__ import absolute_import, print_function, unicode_literals
48 __metaclass__ = type
49
50 import datetime
51+from functools import cmp_to_key
52 from operator import attrgetter
53
54 import apt_pkg
55@@ -559,7 +560,8 @@ class TestGeneralizedPublication(TestCaseWithFactory):
56 '1.1v3',
57 ]
58 spphs = make_spphs_for_versions(self.factory, versions)
59- sorted_spphs = sorted(spphs, cmp=GeneralizedPublication().compare)
60+ sorted_spphs = sorted(
61+ spphs, key=cmp_to_key(GeneralizedPublication().compare))
62 self.assertEqual(
63 sorted(versions), list_source_versions(sorted_spphs))
64
65@@ -572,16 +574,18 @@ class TestGeneralizedPublication(TestCaseWithFactory):
66 ]
67 spphs = make_spphs_for_versions(self.factory, versions)
68
69- debian_sorted_versions = sorted(versions, cmp=apt_pkg.version_compare)
70+ debian_sorted_versions = sorted(
71+ versions, key=cmp_to_key(apt_pkg.version_compare))
72
73 # Assumption: in this case, Debian version ordering is not the
74 # same as alphabetical version ordering.
75 self.assertNotEqual(sorted(versions), debian_sorted_versions)
76
77 # The compare method produces the Debian ordering.
78- sorted_spphs = sorted(spphs, cmp=GeneralizedPublication().compare)
79+ sorted_spphs = sorted(
80+ spphs, key=cmp_to_key(GeneralizedPublication().compare))
81 self.assertEqual(
82- sorted(versions, cmp=apt_pkg.version_compare),
83+ sorted(versions, key=cmp_to_key(apt_pkg.version_compare)),
84 list_source_versions(sorted_spphs))
85
86 def test_compare_breaks_tie_with_creation_date(self):
87@@ -605,7 +609,7 @@ class TestGeneralizedPublication(TestCaseWithFactory):
88
89 self.assertEqual(
90 [spphs[2], spphs[0], spphs[1]],
91- sorted(spphs, cmp=GeneralizedPublication().compare))
92+ sorted(spphs, key=cmp_to_key(GeneralizedPublication().compare)))
93
94 def test_compare_breaks_tie_for_releases_with_same_version(self):
95 # When two publications are tied for comparison because they
96@@ -629,7 +633,7 @@ class TestGeneralizedPublication(TestCaseWithFactory):
97
98 self.assertEqual(
99 [spphs[2], spphs[0], spphs[1]],
100- sorted(spphs, cmp=GeneralizedPublication().compare))
101+ sorted(spphs, key=cmp_to_key(GeneralizedPublication().compare)))
102
103
104 def jumble(ordered_list):
105diff --git a/lib/lp/archiveuploader/tests/nascentupload-announcements.txt b/lib/lp/archiveuploader/tests/nascentupload-announcements.txt
106index 017601f..cd512c3 100644
107--- a/lib/lp/archiveuploader/tests/nascentupload-announcements.txt
108+++ b/lib/lp/archiveuploader/tests/nascentupload-announcements.txt
109@@ -38,14 +38,11 @@ We need to be logged into the security model in order to get any further
110 >>> switch_dbuser('launchpad')
111 >>> login('foo.bar@canonical.com')
112
113-Helper functions to examine emails that were sent:
114+A helper function to examine emails that were sent:
115
116 >>> from lp.services.mail import stub
117 >>> from lp.testing.mail_helpers import pop_notifications
118
119- >>> def by_to_addrs(a, b):
120- ... return cmp(a[1], b[1])
121-
122 >>> def print_addrlist(field):
123 ... for entry in sorted([addr.strip() for addr in field.split(',')]):
124 ... print entry
125diff --git a/lib/lp/bugs/doc/initial-bug-contacts.txt b/lib/lp/bugs/doc/initial-bug-contacts.txt
126index 468658c..cd88e6f 100644
127--- a/lib/lp/bugs/doc/initial-bug-contacts.txt
128+++ b/lib/lp/bugs/doc/initial-bug-contacts.txt
129@@ -157,12 +157,10 @@ track why daf received the email. The rational is repeated in the footer
130 of the email with the bug title and URL.
131
132 >>> import email
133-
134- >>> def by_to_addrs(a, b):
135- ... return cmp(a[1], b[1])
136+ >>> from operator import itemgetter
137
138 >>> test_emails = list(stub.test_emails)
139- >>> test_emails.sort(by_to_addrs)
140+ >>> test_emails.sort(key=itemgetter(1))
141
142 >>> len(test_emails)
143 1
144diff --git a/lib/lp/registry/browser/product.py b/lib/lp/registry/browser/product.py
145index e7dbe7e..36109fe 100644
146--- a/lib/lp/registry/browser/product.py
147+++ b/lib/lp/registry/browser/product.py
148@@ -664,14 +664,12 @@ class ProductSpecificationsMenu(NavigationMenu, ProductEditLinksMixin,
149 'register_sprint']
150
151
152-def _cmp_distros(a, b):
153+def _distro_name_sort_key(name):
154 """Put Ubuntu first, otherwise in alpha order."""
155- if a == 'ubuntu':
156- return -1
157- elif b == 'ubuntu':
158- return 1
159+ if name == 'ubuntu':
160+ return (0, )
161 else:
162- return cmp(a, b)
163+ return (1, name)
164
165
166 class ProductSetBreadcrumb(Breadcrumb):
167@@ -1227,8 +1225,7 @@ class ProductPackagesView(LaunchpadView):
168 distros[distribution.name] = distro
169 distro['packagings'].append(packaging)
170 # Now we sort the resulting list of "distro" objects, and return that.
171- distro_names = distros.keys()
172- distro_names.sort(cmp=_cmp_distros)
173+ distro_names = sorted(distros, key=_distro_name_sort_key)
174 results = [distros[name] for name in distro_names]
175 return results
176
177diff --git a/lib/lp/registry/doc/distribution-mirror.txt b/lib/lp/registry/doc/distribution-mirror.txt
178index 90bc89e..43e129c 100644
179--- a/lib/lp/registry/doc/distribution-mirror.txt
180+++ b/lib/lp/registry/doc/distribution-mirror.txt
181@@ -341,10 +341,11 @@ up on the public mirror listings.
182 >>> transaction.commit()
183
184 >>> import email
185+ >>> from operator import itemgetter
186 >>> from lp.services.mail import stub
187 >>> len(stub.test_emails)
188 2
189- >>> stub.test_emails.sort(lambda a, b: cmp(a[1], b[1])) # sort by to_addr
190+ >>> stub.test_emails.sort(key=itemgetter(1)) # sort by to_addr
191 >>> from_addr, to_addr, raw_message = stub.test_emails.pop()
192 >>> msg = email.message_from_string(raw_message)
193 >>> msg['To']
194diff --git a/lib/lp/registry/doc/teammembership-email-notification.txt b/lib/lp/registry/doc/teammembership-email-notification.txt
195index c1c323f..201cc48 100644
196--- a/lib/lp/registry/doc/teammembership-email-notification.txt
197+++ b/lib/lp/registry/doc/teammembership-email-notification.txt
198@@ -7,8 +7,7 @@ where we might want to notify only the team admins, but in most of the
199 cases we'll be sending two similar (but not identical) notifications:
200 one for all team admins and another for the member.
201
202- >>> def by_to_addrs(a, b):
203- ... return cmp(a[1], b[1])
204+ >>> from operator import itemgetter
205
206 >>> def setStatus(membership, status, reviewer=None, comment=None,
207 ... silent=False):
208@@ -186,7 +185,7 @@ The same goes for approving a proposed member.
209 >>> setStatus(daf_membership, TeamMembershipStatus.APPROVED,
210 ... reviewer=mark, comment='This is a nice guy; I like him')
211 >>> run_mail_jobs()
212- >>> stub.test_emails.sort(by_to_addrs)
213+ >>> stub.test_emails.sort(key=itemgetter(1))
214 >>> len(stub.test_emails)
215 6
216
217@@ -236,7 +235,7 @@ The same for deactivating a membership.
218 >>> setStatus(daf_membership, TeamMembershipStatus.DEACTIVATED,
219 ... reviewer=mark)
220 >>> run_mail_jobs()
221- >>> stub.test_emails.sort(by_to_addrs)
222+ >>> stub.test_emails.sort(key=itemgetter(1))
223 >>> len(stub.test_emails)
224 6
225
226diff --git a/lib/lp/registry/tests/test_distroseriesdifferencecomment.py b/lib/lp/registry/tests/test_distroseriesdifferencecomment.py
227index beeaecd..f95aac7 100644
228--- a/lib/lp/registry/tests/test_distroseriesdifferencecomment.py
229+++ b/lib/lp/registry/tests/test_distroseriesdifferencecomment.py
230@@ -6,7 +6,7 @@
231 __metaclass__ = type
232
233 from datetime import timedelta
234-from random import randint
235+from random import random
236
237 from storm.store import Store
238 from zope.component import getUtility
239@@ -31,14 +31,9 @@ def get_comment_source():
240 return getUtility(IDistroSeriesDifferenceCommentSource)
241
242
243-def flip_coin(*args):
244- """Random comparison function. Returns -1 or 1 randomly."""
245- return 1 - 2 * randint(0, 1)
246-
247-
248 def randomize_list(original_list):
249 """Sort a list (or other iterable) in random order. Return list."""
250- return sorted(original_list, cmp=flip_coin)
251+ return sorted(original_list, key=lambda _: random)
252
253
254 class DistroSeriesDifferenceCommentTestCase(TestCaseWithFactory):
255diff --git a/lib/lp/services/doc/orderingcheck.txt b/lib/lp/services/doc/orderingcheck.txt
256index 9ed2b0b..fe9294f 100644
257--- a/lib/lp/services/doc/orderingcheck.txt
258+++ b/lib/lp/services/doc/orderingcheck.txt
259@@ -29,10 +29,7 @@ Sorting criteria
260 The OrderingCheck accepts all the same sorting options (as keyword args)
261 as Python's built-in sorting functions.
262
263- >>> def sort_cmp(left_item, right_item):
264- ... return left_item - right_item
265-
266- >>> checker = OrderingCheck(cmp=sort_cmp, reverse=True)
267+ >>> checker = OrderingCheck(key=lambda v: v, reverse=True)
268 >>> for number in (3, 2, 1):
269 ... checker.check(number)
270
271diff --git a/lib/lp/services/webapp/sorting.py b/lib/lp/services/webapp/sorting.py
272index dc1a453..6d34142 100644
273--- a/lib/lp/services/webapp/sorting.py
274+++ b/lib/lp/services/webapp/sorting.py
275@@ -16,14 +16,14 @@ import six
276 def expand_numbers(unicode_text, fill_digits=4):
277 """Return a copy of the string with numbers zero filled.
278
279- >>> expand_numbers(u'hello world')
280- u'hello world'
281- >>> expand_numbers(u'0.12.1')
282- u'0000.0012.0001'
283- >>> expand_numbers(u'0.12.1', 2)
284- u'00.12.01'
285- >>> expand_numbers(u'branch-2-3.12')
286- u'branch-0002-0003.0012'
287+ >>> print(expand_numbers(u'hello world'))
288+ hello world
289+ >>> print(expand_numbers(u'0.12.1'))
290+ 0000.0012.0001
291+ >>> print(expand_numbers(u'0.12.1', 2))
292+ 00.12.01
293+ >>> print(expand_numbers(u'branch-2-3.12'))
294+ branch-0002-0003.0012
295
296 """
297 assert(isinstance(unicode_text, six.text_type))
298@@ -40,26 +40,22 @@ reversed_numbers_table = dict(
299 zip(map(ord, u'0123456789'), reversed(u'0123456789')))
300
301
302-def _reversed_number_comparator(lhs_text, rhs_text):
303+def _reversed_number_sort_key(text):
304 """Return comparison value reversed for numbers only.
305
306- >>> _reversed_number_comparator(u'9.3', u'2.4')
307- -1
308- >>> _reversed_number_comparator(u'world', u'hello')
309- 1
310- >>> _reversed_number_comparator(u'hello world', u'hello world')
311- 0
312- >>> _reversed_number_comparator(u'dev', u'development')
313- -1
314- >>> _reversed_number_comparator(u'bzr-0.13', u'bzr-0.08')
315- -1
316+ >>> print(_reversed_number_sort_key(u'9.3'))
317+ 0.6
318+ >>> print(_reversed_number_sort_key(u'2.4'))
319+ 7.5
320+ >>> print(_reversed_number_sort_key(u'hello'))
321+ hello
322+ >>> print(_reversed_number_sort_key(u'bzr-0.13'))
323+ bzr-9.86
324
325 """
326- assert isinstance(lhs_text, six.text_type)
327- assert isinstance(rhs_text, six.text_type)
328- translated_lhs_text = lhs_text.translate(reversed_numbers_table)
329- translated_rhs_text = rhs_text.translate(reversed_numbers_table)
330- return cmp(translated_lhs_text, translated_rhs_text)
331+ assert isinstance(text, six.text_type)
332+ assert isinstance(text, six.text_type)
333+ return text.translate(reversed_numbers_table)
334
335
336 def _identity(x):
337@@ -71,13 +67,13 @@ def sorted_version_numbers(sequence, key=_identity):
338
339 >>> bzr_versions = [u'0.9', u'0.10', u'0.11']
340 >>> for version in sorted_version_numbers(bzr_versions):
341- ... print version
342+ ... print(version)
343 0.11
344 0.10
345 0.9
346 >>> bzr_versions = [u'bzr-0.9', u'bzr-0.10', u'bzr-0.11']
347 >>> for version in sorted_version_numbers(bzr_versions):
348- ... print version
349+ ... print(version)
350 bzr-0.11
351 bzr-0.10
352 bzr-0.9
353@@ -91,7 +87,7 @@ def sorted_version_numbers(sequence, key=_identity):
354 >>> from operator import attrgetter
355 >>> for version in sorted_version_numbers(bzr_versions,
356 ... key=attrgetter('name')):
357- ... print version.name
358+ ... print(version.name)
359 0.11
360 0.10
361 0.9
362@@ -101,9 +97,9 @@ def sorted_version_numbers(sequence, key=_identity):
363 foo
364
365 """
366- expanded_key = lambda x: expand_numbers(key(x))
367- return sorted(sequence, key=expanded_key,
368- cmp=_reversed_number_comparator)
369+ return sorted(
370+ sequence,
371+ key=lambda x: _reversed_number_sort_key(expand_numbers(key(x))))
372
373
374 def sorted_dotted_numbers(sequence, key=_identity):
375@@ -117,13 +113,13 @@ def sorted_dotted_numbers(sequence, key=_identity):
376
377 >>> bzr_versions = [u'0.9', u'0.10', u'0.11']
378 >>> for version in sorted_dotted_numbers(bzr_versions):
379- ... print version
380+ ... print(version)
381 0.9
382 0.10
383 0.11
384 >>> bzr_versions = [u'bzr-0.9', u'bzr-0.10', u'bzr-0.11']
385 >>> for version in sorted_dotted_numbers(bzr_versions):
386- ... print version
387+ ... print(version)
388 bzr-0.9
389 bzr-0.10
390 bzr-0.11
391@@ -137,7 +133,7 @@ def sorted_dotted_numbers(sequence, key=_identity):
392 >>> from operator import attrgetter
393 >>> for version in sorted_dotted_numbers(bzr_versions,
394 ... key=attrgetter('name')):
395- ... print version.name
396+ ... print(version.name)
397 0.9
398 0.10
399 0.11
400@@ -147,5 +143,4 @@ def sorted_dotted_numbers(sequence, key=_identity):
401 foo
402
403 """
404- expanded_key = lambda x: expand_numbers(key(x))
405- return sorted(sequence, key=expanded_key)
406+ return sorted(sequence, key=lambda x: expand_numbers(key(x)))

Subscribers

People subscribed via source and target branches

to status/vote changes: