espnet2.train.class_choices.ClassChoices
Less than 1 minute
espnet2.train.class_choices.ClassChoices
class espnet2.train.class_choices.ClassChoices(name: str, classes: Mapping[str, Type], type_check: Type | None = None, default: str | None = None, optional: bool = False)
Bases: object
Helper class to manage the options for variable objects and its configuration.
Example:
>>> class A:
... def __init__(self, foo=3): pass
>>> class B:
... def __init__(self, bar="aaaa"): pass
>>> choices = ClassChoices("var", dict(a=A, b=B), default="a")
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> choices.add_arguments(parser)
>>> args = parser.parse_args(["--var", "a", "--var_conf", "foo=4")
>>> args.var
a
>>> args.var_conf
{"foo": 4}
>>> class_obj = choices.get_class(args.var)
>>> a_object = class_obj(**args.var_conf)
add_arguments(parser)
choices() → Tuple[str | None, ...]
get_class(name: str | None) → type | None