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 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:
perm = objs
random.shuffle(perm) # N.B. This shuffles the original list.
else: # Select objects by time our count.
return _take(_equal_chance_permutation(objs), number) if time:
return _take_time(perm, time * 60, album)
elif not time: else:
number = min(len(objs), number) return _take(perm, number)
objs = random.sample(objs, number)
return objs
def random_func(lib, opts, args): def random_func(lib, opts, args):