Added an option to define the field to use for equal chance sampling

This commit is contained in:
Sebastian Mohr 2025-08-22 12:00:34 +02:00
parent 15515bec95
commit 9ed6ca613d

View file

@ -29,6 +29,7 @@ def random_func(lib: Library, opts: optparse.Values, args: list[str]):
number=opts.number,
time_minutes=opts.time,
equal_chance=opts.equal_chance,
equal_chance_field=opts.field,
):
print_(format(obj))
@ -55,6 +56,13 @@ random_cmd.parser.add_option(
type="float",
help="total length in minutes of objects to choose",
)
random_cmd.parser.add_option(
"-f",
"--field",
action="store",
type="string",
help="field to use for equal chance sampling (default: albumartist)",
)
random_cmd.parser.add_all_common_options()
random_cmd.func = random_func
@ -124,6 +132,7 @@ def random_objs(
number: int = 1,
time_minutes: float | None = None,
equal_chance: bool = False,
equal_chance_field: str = "albumartist",
) -> Iterable[T]:
"""Get a random subset of items, optionally constrained by time or count.
@ -140,7 +149,7 @@ def random_objs(
# artist-balanced way.
perm: Iterable[T]
if equal_chance:
perm = _equal_chance_permutation(objs)
perm = _equal_chance_permutation(objs, field=equal_chance_field)
else:
perm = list(objs)
random.shuffle(perm)