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:
Adrian Sampson 2016-12-26 17:04:21 -05:00
parent 33eb4ff91b
commit 641e62f2e0

View file

@ -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:
perm = objs
random.shuffle(perm) # N.B. This shuffles the original list.
else:
return _take(_equal_chance_permutation(objs), number)
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):