From 7983c94ef825bd30969b6cf4cda7a330bfa202e1 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 10 Jun 2013 15:40:51 -0700 Subject: [PATCH] add keys() method to Distance --- beets/autotag/hooks.py | 16 +++++++++------- beets/autotag/match.py | 4 ++-- beets/ui/commands.py | 2 +- test/test_autotag.py | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index 6156abf2c..5938c2308 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -267,7 +267,7 @@ class Distance(object): @property def distance(self): - """Returns a weighted and normalised distance across all + """Return a weighted and normalized distance across all penalties. """ dist_max = self.max_distance @@ -277,7 +277,7 @@ class Distance(object): @property def max_distance(self): - """Returns the maximum distance penalty. + """Return the maximum distance penalty (normalization factor). """ dist_max = 0.0 for key, penalty in self._penalties.iteritems(): @@ -286,16 +286,15 @@ class Distance(object): @property def raw_distance(self): - """Returns the raw (denormalized) distance. + """Return the raw (denormalized) distance. """ dist_raw = 0.0 for key, penalty in self._penalties.iteritems(): dist_raw += sum(penalty) * self._weights[key] return dist_raw - @property def items(self): - """Returns a list of (key, dist) pairs, with `dist` being the + """Return 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. """ @@ -336,10 +335,13 @@ class Distance(object): return 0.0 def __iter__(self): - return iter(self.items) + return iter(self.items()) def __len__(self): - return len(self.items) + return len(self.items()) + + def keys(self): + return [key for key, _ in self.items()] def update(self, dist): """Adds all the distance penalties from `dist`. diff --git a/beets/autotag/match.py b/beets/autotag/match.py index 630a0de55..1f1ba2d43 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -291,10 +291,10 @@ def _recommendation(results): # Downgrade to the max rec if it is lower than the current rec for an # applied penalty. - keys = set(key for key, _ in min_dist) + keys = set(min_dist.keys()) if isinstance(results[0], hooks.AlbumMatch): for track_dist in min_dist.tracks.values(): - keys.update(key for key, _ in track_dist) + keys.update(track_dist.keys()) for key in keys: max_rec = config['match']['max_rec'][key].as_choice({ 'strong': recommendation.strong, diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 0512cb585..f9e3bc6eb 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -168,7 +168,7 @@ def penalty_string(distance, limit=None): applied to a distance object. """ penalties = [] - for key, _ in distance: + for key in distance.keys(): key = key.replace('album_', '') key = key.replace('track_', '') key = key.replace('_', ' ') diff --git a/test/test_autotag.py b/test/test_autotag.py index 889584e0c..4684f9719 100644 --- a/test/test_autotag.py +++ b/test/test_autotag.py @@ -239,7 +239,7 @@ class DistanceTest(_common.TestCase): dist = Distance() dist.add('album', 0.1875) dist.add('medium', 0.75) - self.assertEqual(dist.items, [('medium', 0.25), ('album', 0.125)]) + self.assertEqual(dist.items(), [('medium', 0.25), ('album', 0.125)]) # Sort by key if distance is equal. dist = Distance()