From ae0b070c1b89a059e7a518adbdf9cc637244a93d Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Wed, 27 Jun 2018 13:24:59 +0100 Subject: [PATCH] [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. --- deluge/core/filtermanager.py | 18 ++++++------------ .../console/modes/torrentlist/torrentview.py | 17 ++--------------- 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/deluge/core/filtermanager.py b/deluge/core/filtermanager.py index 09d3e1e2a..d669564f4 100644 --- a/deluge/core/filtermanager.py +++ b/deluge/core/filtermanager.py @@ -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 diff --git a/deluge/ui/console/modes/torrentlist/torrentview.py b/deluge/ui/console/modes/torrentlist/torrentview.py index 50bb25d31..e81ba6c99 100644 --- a/deluge/ui/console/modes/torrentlist/torrentview.py +++ b/deluge/ui/console/modes/torrentlist/torrentview.py @@ -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