diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index c29cf576f..6156abf2c 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -294,8 +294,8 @@ class Distance(object): return dist_raw @property - def sorted(self): - """Returns a list of (dist, key) pairs, with `dist` being the + def items(self): + """Returns a list of (key, dist) pairs, with `dist` being the weighted distance, sorted from highest to lowest. Does not include penalties with a zero value. """ @@ -303,11 +303,11 @@ class Distance(object): for key in self._penalties: dist = self[key] if dist: - list_.append((dist, key)) - # Convert distance into a negative float we can sort items in ascending - # order (for keys, when the penalty is equal) and still get the items - # with the biggest distance first. - return sorted(list_, key=lambda (dist, key): (0-dist, key)) + list_.append((key, dist)) + # Convert distance into a negative float we can sort items in + # ascending order (for keys, when the penalty is equal) and + # still get the items with the biggest distance first. + return sorted(list_, key=lambda (key, dist): (0-dist, key)) # Behave like a float. @@ -336,10 +336,10 @@ class Distance(object): return 0.0 def __iter__(self): - return iter(self.sorted) + return iter(self.items) def __len__(self): - return len(self.sorted) + return len(self.items) def update(self, dist): """Adds all the distance penalties from `dist`. diff --git a/beets/ui/commands.py b/beets/ui/commands.py index dfe3585c1..0512cb585 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -164,11 +164,11 @@ def dist_string(dist): return out def penalty_string(distance, limit=None): - """Returns a colorized string that indicates all the penalties applied to - a distance object. + """Returns a colorized string that indicates all the penalties + applied to a distance object. """ penalties = [] - for _, key in distance: + for key, _ in distance: key = key.replace('album_', '') key = key.replace('track_', '') key = key.replace('_', ' ') diff --git a/test/test_autotag.py b/test/test_autotag.py index 4a1eba17b..889584e0c 100644 --- a/test/test_autotag.py +++ b/test/test_autotag.py @@ -216,7 +216,7 @@ class DistanceTest(_common.TestCase): dist.add('medium', 0.25) dist.add('medium', 0.75) self.assertEqual(len(dist), 2) - self.assertEqual(list(dist), [(0.2, 'album'), (0.2, 'medium')]) + self.assertEqual(list(dist), [('album', 0.2), ('medium', 0.2)]) self.assertTrue(dist == 0.4) self.assertTrue(dist < 1.0) self.assertTrue(dist > 0.0) @@ -233,19 +233,19 @@ class DistanceTest(_common.TestCase): dist.add('medium', 0.5) self.assertEqual(dist.raw_distance, 2.25) - def test_sorted(self): + def test_items(self): config['match']['distance_weights']['album'] = 4.0 config['match']['distance_weights']['medium'] = 2.0 dist = Distance() dist.add('album', 0.1875) dist.add('medium', 0.75) - self.assertEqual(dist.sorted, [(0.25, 'medium'), (0.125, 'album')]) + self.assertEqual(dist.items, [('medium', 0.25), ('album', 0.125)]) # Sort by key if distance is equal. dist = Distance() dist.add('album', 0.375) dist.add('medium', 0.75) - self.assertEqual(dist.sorted, [(0.25, 'album'), (0.25, 'medium')]) + self.assertEqual(dist.items, [('album', 0.25), ('medium', 0.25)]) def test_update(self): dist1 = Distance()