Mercurial > p > roundup > code
changeset 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 | 7a41dd2abbbd |
| children | 2d6a92c3e212 |
| files | roundup/admin.py roundup/cgi/form_parser.py roundup/cgi/templating.py roundup/date.py roundup/password.py roundup/security.py scripts/contributors.py scripts/import_sf.py scripts/roundup-reminder test/test_templating.py |
| diffstat | 10 files changed, 135 insertions(+), 56 deletions(-) [+] |
line wrap: on
line diff
--- a/roundup/admin.py Wed Jul 25 00:37:54 2018 +0000 +++ b/roundup/admin.py Wed Jul 25 00:39:37 2018 +0000 @@ -153,7 +153,7 @@ """ Produce an HTML command list. """ commands = sorted(iter(self.commands.values()), - operator.attrgetter('__name__')) + key=operator.attrgetter('__name__')) for command in commands: h = _(command.__doc__).split('\n') name = command.__name__[3:]
--- a/roundup/cgi/form_parser.py Wed Jul 25 00:37:54 2018 +0000 +++ b/roundup/cgi/form_parser.py Wed Jul 25 00:39:37 2018 +0000 @@ -467,7 +467,7 @@ value = existing # Sort the value in the same order used by # Multilink.from_raw. - value.sort(lambda x, y: cmp(int(x),int(y))) + value.sort(key=int) elif value == '': # other types should be None'd if there's no value @@ -511,7 +511,7 @@ # The canonical order (given in Multilink.from_raw) is # by the numeric value of the IDs. if isinstance(proptype, hyperdb.Multilink): - existing.sort(lambda x, y: cmp(int(x),int(y))) + existing.sort(key=int) # "missing" existing values may not be None if not existing:
--- a/roundup/cgi/templating.py Wed Jul 25 00:37:54 2018 +0000 +++ b/roundup/cgi/templating.py Wed Jul 25 00:39:37 2018 +0000 @@ -620,7 +620,7 @@ l.append(htmlklass(self._client, self._classname, '', prop, name, value, self._anonymous)) if sort: - l.sort(lambda a,b:cmp(a._name, b._name)) + l.sort(key=lambda a:a._name) return l def list(self, sort_on=None): @@ -628,8 +628,8 @@ """ # get the list and sort it nicely l = self._klass.list() - sortfunc = make_sort_function(self._db, self._classname, sort_on) - l.sort(sortfunc) + keyfunc = make_key_function(self._db, self._classname, sort_on) + l.sort(key=keyfunc) # check perms check = self._client.db.security.hasPermission @@ -1338,10 +1338,30 @@ self._prop, self._value) def __str__(self): return self.plain() - def __cmp__(self, other): + def __lt__(self, other): + if isinstance(other, HTMLProperty): + return self._value < other._value + return self._value < other + def __le__(self, other): + if isinstance(other, HTMLProperty): + return self._value <= other._value + return self._value <= other + def __eq__(self, other): if isinstance(other, HTMLProperty): - return cmp(self._value, other._value) - return cmp(self._value, other) + return self._value == other._value + return self._value == other + def __ne__(self, other): + if isinstance(other, HTMLProperty): + return self._value != other._value + return self._value != other + def __gt__(self, other): + if isinstance(other, HTMLProperty): + return self._value > other._value + return self._value > other + def __ge__(self, other): + if isinstance(other, HTMLProperty): + return self._value >= other._value + return self._value >= other def __bool__(self): return not not self._value @@ -2258,12 +2278,12 @@ if self._value: display_value = lookupIds(self._db, self._prop, self._value, fail_ok=1, do_lookup=False) - sortfun = make_sort_function(self._db, self._prop.classname) + keyfun = make_key_function(self._db, self._prop.classname) # sorting fails if the value contains # items not yet stored in the database # ignore these errors to preserve user input try: - display_value.sort(sortfun) + display_value.sort(key=keyfun) except: pass self._value = display_value @@ -2303,7 +2323,7 @@ def sorted(self, property): """ Return this multilink sorted by the given property """ value = list(self.__iter__()) - value.sort(lambda a,b:cmp(a[property], b[property])) + value.sort(key=lambda a:a[property]) return value def __contains__(self, value): @@ -2501,21 +2521,19 @@ propclasses.append((prop, cls)) -def make_sort_function(db, classname, sort_on=None): - """Make a sort function for a given class. +def make_key_function(db, classname, sort_on=None): + """Make a sort key function for a given class. The list being sorted may contain mixed ids and labels. """ linkcl = db.getclass(classname) if sort_on is None: sort_on = linkcl.orderprop() - def sortfunc(a, b): + def keyfunc(a): if num_re.match(a): a = linkcl.get(a, sort_on) - if num_re.match(b): - b = linkcl.get(b, sort_on) - return cmp(a, b) - return sortfunc + return a + return keyfunc def handleListCGIValue(value): """ Value is either a single item or a list of items. Each item has a
--- a/roundup/date.py Wed Jul 25 00:37:54 2018 +0000 +++ b/roundup/date.py Wed Jul 25 00:39:37 2018 +0000 @@ -30,6 +30,13 @@ except ImportError: pytz = None +try: + cmp +except NameError: + # Python 3. + def cmp(a, b): + return (a > b) - (a < b) + from roundup import i18n # no, I don't know why we must anchor the date RE when we only ever use it @@ -585,6 +592,19 @@ return cmp(int(self.second), int(other.second)) return cmp(self.second, other.second) + def __lt__(self, other): + return self.__cmp__(other) < 0 + def __le__(self, other): + return self.__cmp__(other) <= 0 + def __eq__(self, other): + return self.__cmp__(other) == 0 + def __ne__(self, other): + return self.__cmp__(other) != 0 + def __gt__(self, other): + return self.__cmp__(other) > 0 + def __ge__(self, other): + return self.__cmp__(other) >= 0 + def __str__(self): """Return this date as a string in the yyyy-mm-dd.hh:mm:ss format.""" return self.formal() @@ -834,13 +854,53 @@ y = now - (date + self) self.__init__(y.get_tuple()) - def __cmp__(self, other): + def __lt__(self, other): + """Compare this interval to another interval.""" + + if other is None: + # we are always larger than None + return False + return self.as_seconds() < other.as_seconds() + + def __le__(self, other): + """Compare this interval to another interval.""" + + if other is None: + # we are always larger than None + return False + return self.as_seconds() <= other.as_seconds() + + def __eq__(self, other): """Compare this interval to another interval.""" if other is None: # we are always larger than None - return 1 - return cmp(self.as_seconds(), other.as_seconds()) + return False + return self.as_seconds() == other.as_seconds() + + def __ne__(self, other): + """Compare this interval to another interval.""" + + if other is None: + # we are always larger than None + return True + return self.as_seconds() != other.as_seconds() + + def __gt__(self, other): + """Compare this interval to another interval.""" + + if other is None: + # we are always larger than None + return True + return self.as_seconds() > other.as_seconds() + + def __ge__(self, other): + """Compare this interval to another interval.""" + + if other is None: + # we are always larger than None + return True + return self.as_seconds() >= other.as_seconds() def __str__(self): """Return this interval as a string."""
--- a/roundup/password.py Wed Jul 25 00:37:54 2018 +0000 +++ b/roundup/password.py Wed Jul 25 00:39:37 2018 +0000 @@ -223,19 +223,22 @@ __str__ = dummystr - def __cmp__(self, other): + def __eq__(self, other): """Compare this password against another password.""" # check to see if we're comparing instances if isinstance(other, self.__class__): if self.scheme != other.scheme: - return cmp(self.scheme, other.scheme) - return cmp(self.password, other.password) + return False + return self.password == other.password # assume password is plaintext if self.password is None: raise ValueError('Password not set') - return cmp(self.password, encodePassword(other, self.scheme, - self.password or None)) + return self.password == encodePassword(other, self.scheme, + self.password or None) + + def __ne__(self, other): + return not self.__eq__(other) class Password(JournalPassword): """The class encapsulates a Password property type value in the database.
--- a/roundup/security.py Wed Jul 25 00:37:54 2018 +0000 +++ b/roundup/security.py Wed Jul 25 00:39:37 2018 +0000 @@ -155,18 +155,21 @@ self.klass, self.properties, self.check, self.limit_perm_to_props_only) - def __cmp__(self, other): + def __eq__(self, other): if self.name != other.name: - return cmp(self.name, other.name) + return False - if self.klass != other.klass: return 1 - if self.properties != other.properties: return 1 - if self.check != other.check: return 1 + if self.klass != other.klass: return False + if self.properties != other.properties: return False + if self.check != other.check: return False if self.limit_perm_to_props_only != \ - other.limit_perm_to_props_only: return 1 + other.limit_perm_to_props_only: return False # match - return 0 + return True + + def __ne__(self, other): + return not self.__eq__(other) def __getitem__(self,index): return (self.name, self.klass, self.properties, self.check,
--- a/scripts/contributors.py Wed Jul 25 00:37:54 2018 +0000 +++ b/scripts/contributors.py Wed Jul 25 00:39:37 2018 +0000 @@ -136,19 +136,16 @@ """Return year of the first contribution""" return sorted(list(names[name]))[0] - def year_cmp(name1, name2): + def year_key(name): """ - Year comparison function. First sort by latest contribution year (desc). + Year key function. First sort by latest contribution year (desc). If it matches, compare first contribution year (asc). This ensures that the most recent and long-term contributors are at the top. """ - if last_year(name1) != last_year(name2): - return last_year(name1) - last_year(name2) - else: - return first_year(name2) - first_year(name1) + return (last_year(name), -first_year(name)) print("Copyright (c)") - for author in sorted(list(names), cmp=year_cmp, reverse=True): + for author in sorted(list(names), key=year_key, reverse=True): years = list(names[author]) yearstr = compress(years)
--- a/scripts/import_sf.py Wed Jul 25 00:37:54 2018 +0000 +++ b/scripts/import_sf.py Wed Jul 25 00:39:37 2018 +0000 @@ -279,7 +279,7 @@ # sort messages and assign ids d['messages'] = [] - message_data.sort(lambda a,b:cmp(a['date'],b['date'])) + message_data.sort(key=lambda a:a['date']) for message in message_data: message_id += 1 message['id'] = str(message_id)
--- a/scripts/roundup-reminder Wed Jul 25 00:37:54 2018 +0000 +++ b/scripts/roundup-reminder Wed Jul 25 00:39:37 2018 +0000 @@ -41,16 +41,9 @@ resolved_id = db.status.lookup('resolved') -def listCompare(x, y): - "compare two tuples such that order is positive on [0] and negative on [1]" - if x[0] < y[0]: - return -1 - if x[0] > y[0]: - return 1 - if x[1] > y[1]: - return -1 - if x[1] < y[1]: - return 1 +def listKey(x): + "key for tuples such that order is positive on [0] and negative on [1]" + return (x[0], -x[1]) return 0 # loop through all the users @@ -73,7 +66,7 @@ db.issue.get(issue_id, 'creation'), issue_id)) # sort the issues by timeliness and creation date - l.sort(listCompare) + l.sort(key=listKey) if not l: continue
--- a/test/test_templating.py Wed Jul 25 00:37:54 2018 +0000 +++ b/test/test_templating.py Wed Jul 25 00:39:37 2018 +0000 @@ -354,7 +354,12 @@ def __init__(self, client, classname, nodeid, prop, name, value, def __repr__(self): def __str__(self): - def __cmp__(self, other): + def __lt__(self, other): + def __le__(self, other): + def __eq__(self, other): + def __ne__(self, other): + def __gt__(self, other): + def __ge__(self, other): def is_edit_ok(self): def is_view_ok(self): @@ -418,8 +423,8 @@ def field(self, size=30, showid=0): def menu(self, size=None, height=None, showid=0, additional=[], -def make_sort_function(db, classname, sort_on=None): - def sortfunc(a, b): +def make_key_function(db, classname, sort_on=None): + def keyfunc(a): def find_sort_key(linkcl):
