mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 16:42:42 +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
|
artist an equal chance of being included so that artists with more
|
||||||
songs are not represented disproportionately.
|
songs are not represented disproportionately.
|
||||||
"""
|
"""
|
||||||
if time:
|
# Permute the objects either in a straightforward way or an
|
||||||
time_sec = time * 60
|
# artist-balanced way.
|
||||||
objs_shuffled = objs
|
|
||||||
random.shuffle(objs_shuffled)
|
|
||||||
|
|
||||||
if not equal_chance:
|
|
||||||
return _take_time(objs_shuffled, time_sec, album)
|
|
||||||
|
|
||||||
if equal_chance:
|
if equal_chance:
|
||||||
if time:
|
perm = _equal_chance_permutation(objs)
|
||||||
return _take_time(_equal_chance_permutation(objs), time_sec, album)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return _take(_equal_chance_permutation(objs), number)
|
perm = objs
|
||||||
|
random.shuffle(perm) # N.B. This shuffles the original list.
|
||||||
|
|
||||||
elif not time:
|
# Select objects by time our count.
|
||||||
number = min(len(objs), number)
|
if time:
|
||||||
objs = random.sample(objs, number)
|
return _take_time(perm, time * 60, album)
|
||||||
|
else:
|
||||||
return objs
|
return _take(perm, number)
|
||||||
|
|
||||||
|
|
||||||
def random_func(lib, opts, args):
|
def random_func(lib, opts, args):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue