Mercurial > p > roundup > code
comparison roundup/cgi/form_parser.py @ 5414:3fa026621f69
Python 3 preparation: comparisons.
Python 3 no longer has the cmp function, or cmp= arguments to sorting
functions / methods (key= must be used instead), and requires rich
comparison methods such as __lt__ to be defined instead of using
__cmp__. All of the comparison mechanisms supported in Python 3 are
also supported in Python 2.
This patch makes the corresponding changes in Roundup to use key
functions and rich comparison methods. In the case of the
JournalPassword and Permission classes, only __eq__ and __ne__ are
defined as I don't see ordered comparisons as useful there (and for
Permission, the old __cmp__ function didn't try to provide a valid
ordering). In the case of the Date class, I kept the __cmp__ method
and implemented the others in terms of it, to avoid excess
repetitiveness in duplicating implementation code for all six rich
comparison methods.
In roundup/admin.py, help_commands_html used operator.attrgetter to
produce the second argument of sorted() - which would be reasonable
for a key function, but the second argument is the cmp function in
Python 2, not a key function (and the key function must be a named
argument not a positional argument in Python 3). That function
appears to be completely unused, so I expect that code never worked.
This patch adds the missing key= to that sorted() call, but it would
also be reasonable to remove the unused function completely instead.
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Wed, 25 Jul 2018 00:39:37 +0000 |
| parents | 99667a0cbd2d |
| children | c5c33e62da39 |
comparison
equal
deleted
inserted
replaced
| 5413:7a41dd2abbbd | 5414:3fa026621f69 |
|---|---|
| 465 if entry not in existing: | 465 if entry not in existing: |
| 466 existing.append(entry) | 466 existing.append(entry) |
| 467 value = existing | 467 value = existing |
| 468 # Sort the value in the same order used by | 468 # Sort the value in the same order used by |
| 469 # Multilink.from_raw. | 469 # Multilink.from_raw. |
| 470 value.sort(lambda x, y: cmp(int(x),int(y))) | 470 value.sort(key=int) |
| 471 | 471 |
| 472 elif value == '': | 472 elif value == '': |
| 473 # other types should be None'd if there's no value | 473 # other types should be None'd if there's no value |
| 474 value = None | 474 value = None |
| 475 else: | 475 else: |
| 509 # be sure to use the same sort order in all places, | 509 # be sure to use the same sort order in all places, |
| 510 # since we want to compare values with "=" or "!=". | 510 # since we want to compare values with "=" or "!=". |
| 511 # The canonical order (given in Multilink.from_raw) is | 511 # The canonical order (given in Multilink.from_raw) is |
| 512 # by the numeric value of the IDs. | 512 # by the numeric value of the IDs. |
| 513 if isinstance(proptype, hyperdb.Multilink): | 513 if isinstance(proptype, hyperdb.Multilink): |
| 514 existing.sort(lambda x, y: cmp(int(x),int(y))) | 514 existing.sort(key=int) |
| 515 | 515 |
| 516 # "missing" existing values may not be None | 516 # "missing" existing values may not be None |
| 517 if not existing: | 517 if not existing: |
| 518 if isinstance(proptype, hyperdb.String): | 518 if isinstance(proptype, hyperdb.String): |
| 519 # some backends store "missing" Strings as empty strings | 519 # some backends store "missing" Strings as empty strings |
