mirror of
https://github.com/beetbox/beets.git
synced 2025-12-27 11:02:43 +01:00
replace Distance.sorted() with .items()
This is an effort to make the distance object feel slightly more dict-like. The name changed and order of tuples is reversed: we now yield (key, value) instead of (value, key), which I think is a little more intuitive.
This commit is contained in:
parent
c85e43ee2a
commit
c818663539
3 changed files with 16 additions and 16 deletions
|
|
@ -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`.
|
||||
|
|
|
|||
|
|
@ -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('_', ' ')
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue