[Py3] Fix sort and sorted issues

In Python 3 there is only the key functions available and cmp functions
should be removed, especially for speed.
This commit is contained in:
Calum Lind 2018-06-27 13:24:59 +01:00
parent c3a2c67b98
commit ae0b070c1b
2 changed files with 8 additions and 27 deletions

View File

@ -215,7 +215,7 @@ class FilterManager(component.Component):
sorted_items = {field: sorted(items[field].items()) for field in tree_keys}
if 'state' in tree_keys:
sorted_items['state'].sort(self._sort_state_items)
sorted_items['state'].sort(key=self._sort_state_item)
return sorted_items
@ -255,14 +255,8 @@ class FilterManager(component.Component):
if value != 'All' and count == 0:
del state_items[value]
def _sort_state_items(self, x, y):
if x[0] in STATE_SORT:
ix = STATE_SORT.index(x[0])
else:
ix = 99
if y[0] in STATE_SORT:
iy = STATE_SORT.index(y[0])
else:
iy = 99
return ix - iy
def _sort_state_item(self, item):
try:
return STATE_SORT.index(item[0])
except ValueError:
return 99

View File

@ -38,19 +38,6 @@ state_fg_colors = {
}
def _queue_sort(v1, v2):
if v1 == v2:
return 0
if v2 < 0:
return -1
if v1 < 0:
return 1
if v1 > v2:
return 1
if v2 > v1:
return -1
reverse_sort_fields = [
'size',
'download_speed',
@ -247,7 +234,7 @@ class TorrentView(InputKeyHandler):
# Not a string.
return state.get(s)[field]
to_sort = sorted(to_sort, _queue_sort, sort_key, reverse)
to_sort = sorted(to_sort, key=sort_key, reverse=reverse)
if field == 'eta':
to_sort = sorted(to_sort, key=lambda s: state.get(s)['eta'] == 0)
@ -263,7 +250,7 @@ class TorrentView(InputKeyHandler):
result = sort_by_field(state, result, s_primary)
if self.config['torrentview']['separate_complete']:
result = sorted(result, _queue_sort, lambda s: state.get(s).get('progress', 0) == 100.0)
result = sorted(result, key=lambda s: state.get(s).get('progress', 0) == 100.0)
return result