entoscope.script¶
Experiment scripts.
experiment is importable from entoscope.script and from entoscope.
- entoscope.script.experiment.experiment(func: Callable | None = None, *, description: str | None = None) Callable[source]¶
Turn
main(config: Config)into a command-line entry point.The config dataclass is taken from the first parameter’s annotation and its fields become
--field-nameoptions (seeentoscope.script.argument.build_parser). When the decorated function lives in the__main__module it runs immediately at decoration time, so the config class andDESCRIPTIONmust be defined above it in the script.Usable bare,
@experiment, or with arguments,@experiment(description=...).- Parameters:
func – The experiment function, when used bare.
description – The parser’s description. Defaults to a module-level
DESCRIPTION, else the function’s docstring.
- Returns:
The wrapped function, or a decorator when called with arguments only. The wrapper parses the command line into a config and calls the function with it. It stays callable with an explicit config,
main(Config(...)), which bypasses the parser, or withmain(argv=[...]), which feeds it.- Raises:
TypeError – If the function’s first parameter is not a config dataclass, or it has another required parameter.
- class entoscope.script.argument.DictAction(option_strings, dest, key_type=str, value_type=str, **kwargs)[source]¶
Bases:
ActionCollects
key:valuetokens into a dict, converting each side with its own type.Tokens split on the first
:only, so values may contain colons. Each occurrence of the option builds a fresh dict (the last one wins), leaving any default dict untouched.- Parameters:
option_strings – The option’s flags, as argparse passes them.
dest – The attribute to store the dict in.
key_type – Converts each key.
value_type – Converts each value.
**kwargs – The remaining
add_argumentarguments.
- class entoscope.script.argument.TupleAction(option_strings, dest, element_types=(str, ...), **kwargs)[source]¶
Bases:
ActionCollects values into a tuple, converting each position with its own type.
- Parameters:
option_strings – The option’s flags, as argparse passes them.
dest – The attribute to store the tuple in.
element_types –
typing.get_argsof atupleannotation:(int, str)converts exactly two values positionally, and(int, Ellipsis)converts any number of values withint.**kwargs – The remaining
add_argumentarguments.
- entoscope.script.argument.build_parser(config_cls: type, description: str | None = None) ArgumentParser[source]¶
Build an
ArgumentParserwith one--field-nameoption per dataclass field.Type, default and required-ness are derived from the field itself, and anything argparse-specific that cannot be derived (
help,choices, …) is read fromfield.metadataand overrides the derived arguments.list[X]fields becomenargs="*"options whose values are converted withX,dict[K, V]fieldsnargs="*"options takingkey:valuetokens (seeDictAction),tuple[X, Y]fields take exactly that many values converted per position, andtuple[X, ...]any number (seeTupleAction). Fields with a default get(default: ...)appended to their help text. Put%(default)sin the metadata help to control its placement instead.- Parameters:
config_cls – The config dataclass.
description – The parser’s description.
- Returns:
The parser.
- entoscope.script.argument.parse_config(config_cls: type, argv: Sequence[str] | None = None, description: str | None = None)[source]¶
Parse a command line into a config.
- Parameters:
config_cls – The config dataclass.
argv – The arguments,
sys.argv[1:]whenNone.description – The parser’s description, shown by
--help.
- Returns:
An instance of
config_cls.