comparison scripts/contributors.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 64daaa4bf816
children ce171c81d823
comparison
equal deleted inserted replaced
5413:7a41dd2abbbd 5414:3fa026621f69
134 134
135 def first_year(name): 135 def first_year(name):
136 """Return year of the first contribution""" 136 """Return year of the first contribution"""
137 return sorted(list(names[name]))[0] 137 return sorted(list(names[name]))[0]
138 138
139 def year_cmp(name1, name2): 139 def year_key(name):
140 """ 140 """
141 Year comparison function. First sort by latest contribution year (desc). 141 Year key function. First sort by latest contribution year (desc).
142 If it matches, compare first contribution year (asc). This ensures that 142 If it matches, compare first contribution year (asc). This ensures that
143 the most recent and long-term contributors are at the top. 143 the most recent and long-term contributors are at the top.
144 """ 144 """
145 if last_year(name1) != last_year(name2): 145 return (last_year(name), -first_year(name))
146 return last_year(name1) - last_year(name2)
147 else:
148 return first_year(name2) - first_year(name1)
149 146
150 print("Copyright (c)") 147 print("Copyright (c)")
151 for author in sorted(list(names), cmp=year_cmp, reverse=True): 148 for author in sorted(list(names), key=year_key, reverse=True):
152 years = list(names[author]) 149 years = list(names[author])
153 yearstr = compress(years) 150 yearstr = compress(years)
154 151
155 if 0: #DEBUG 152 if 0: #DEBUG
156 print(years, yearstr, author) 153 print(years, yearstr, author)

Roundup Issue Tracker: http://roundup-tracker.org/