mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 08:39:17 +01:00
Vastly simplify main random function
This is the payoff from the earlier refactorings: the control flow is now consistent and clear, and the two factors (time vs. number, equal-chance or not) are orthogonal. See also #2322.
This commit is contained in:
parent
33eb4ff91b
commit
641e62f2e0
1 changed files with 11 additions and 18 deletions
|
|
@ -97,26 +97,19 @@ def random_objs(objs, album, number=1, time=None, equal_chance=False):
|
|||
artist an equal chance of being included so that artists with more
|
||||
songs are not represented disproportionately.
|
||||
"""
|
||||
if time:
|
||||
time_sec = time * 60
|
||||
objs_shuffled = objs
|
||||
random.shuffle(objs_shuffled)
|
||||
|
||||
if not equal_chance:
|
||||
return _take_time(objs_shuffled, time_sec, album)
|
||||
|
||||
# Permute the objects either in a straightforward way or an
|
||||
# artist-balanced way.
|
||||
if equal_chance:
|
||||
if time:
|
||||
return _take_time(_equal_chance_permutation(objs), time_sec, album)
|
||||
|
||||
perm = _equal_chance_permutation(objs)
|
||||
else:
|
||||
return _take(_equal_chance_permutation(objs), number)
|
||||
perm = objs
|
||||
random.shuffle(perm) # N.B. This shuffles the original list.
|
||||
|
||||
elif not time:
|
||||
number = min(len(objs), number)
|
||||
objs = random.sample(objs, number)
|
||||
|
||||
return objs
|
||||
# Select objects by time our count.
|
||||
if time:
|
||||
return _take_time(perm, time * 60, album)
|
||||
else:
|
||||
return _take(perm, number)
|
||||
|
||||
|
||||
def random_func(lib, opts, args):
|
||||
|
|
|
|||
Loading…
Reference in a new issue