neuralib.argp.core
- class neuralib.argp.core.AbstractParser[source]
- USAGE: str = None
parser usage.
- DESCRIPTION: str = None
parser description.
- EPILOG: str = None
parser epilog. Could be override as a method if its content is dynamic-generated.
- classmethod new_parser(**kwargs)[source]
create an
argparse.ArgumentParser.class variable:
USAGE,DESCRIPTIONandEPILOGare used when creation.>>> class A(AbstractParser): ... @classmethod ... def new_parser(cls, **kwargs) -> argparse.ArgumentParser: ... return super().new_parser(**kwargs)
- Parameters:
kwargs – keyword parameters to ArgumentParser
- Returns:
an ArgumentParser.
- Return type:
ArgumentParser
- main(args=None, *, exit_on_error=True)[source]
parsing the commandline input args and set the argument attributes, then call
run().Example
if overwrite with the argument default, use args
>>> AbstractParser().main((['--source=allen_mouse_25um', '--region=VISal,VISam,...'], []))
- Parameters:
args (list[str] | tuple[list[str]] | None) – commandline arguments, or a tuple of (prepend, append) arguments
exit_on_error (bool) – exit when commandline parsed fail. Otherwise, raise a
RuntimeError.
- neuralib.argp.core.new_parser(instance, reset=False, **kwargs)[source]
Create
ArgumentParserfor instance.- Parameters:
instance (T | type[T])
reset – reset argument attributes. do nothing if instance isn’t an instance.
kwargs – keywords for creating
argparse.ArgumentParser.
- Returns:
- Return type:
ArgumentParser
- neuralib.argp.core.new_command_parser(parsers, usage=None, description=None, reset=False)[source]
Create
ArgumentParserforAbstractParsers.- Parameters:
parsers (dict[str, AbstractParser | type[AbstractParser]]) – dict of command to
AbstractParser.usage (str | None) – parser usage
description (str | None) – parser description
reset – reset argument attributes. do nothing if parsers’s value isn’t an instance.
- Returns:
- Return type:
ArgumentParser
- neuralib.argp.core.parse_args(instance, args=None)[source]
parsing the commandline input args and set the argument attributes.
- Parameters:
instance (T)
args (list[str] | None) – commandline inputs
- Returns:
- Return type:
T
- neuralib.argp.core.parse_command_args(parsers, args=None, usage=None, description=None, run_main=True)[source]
Create
argparse.ArgumentParserforAbstractParsers. Then parsing the commandline input args and setting up correspondAbstractParser.- Parameters:
parsers (dict[str, AbstractParser | type[AbstractParser]]) – dict of command to
AbstractParser.args (list[str] | None) – commandline inputs
usage (str | None) – parser usage
description (str | None) – parser description.
run_main – run
run()
- Returns:
used
AbstractParser- Return type:
AbstractParser | None
- neuralib.argp.core.set_options(instance, result)[source]
set argument attributes from
argparse.Namespace.- Parameters:
instance (T)
result (Namespace)
- Returns:
instance itself.
- Return type:
T
- neuralib.argp.core.argument(*options: str, action: Literal['store', 'store_const', 'store_true', 'store_false', 'append', 'append_const', 'extend', 'count', 'help', 'version', 'boolean'] = ..., nargs: int | Literal['*', '+', '?', '...'] = ..., const: T = ..., default: T = ..., type: Type | Callable[[str], T] = ..., validator: Callable[[T], bool] = ..., validate_on_set: bool = True, choices: Sequence[str] = ..., required: bool = False, hidden: bool = False, help: str = ..., group: str = None, ex_group: str = None, metavar: str = ...) T[source]
create an argument attribute.
Example:
>>> class Example: ... # create a bool flag ... bool_flag: bool = argument('-f') ... # create a single value option ... str_value: str = argument('-a', metavar='VALUE') ... # create a single value option with type auto-casting ... int_value: int = argument('-i', metavar='VALUE') ... # create a position argument ... pos_value: str = argument(metavar='VALUE') ... # create a multiple value option ... list_value: list[str] = argument('-l', metavar='VALUE', nargs=2, action='append')
- Parameters:
kwargs – Please see
argparse.ArgumentParser.add_argumentfor detailed.
- neuralib.argp.core.as_argument(a)[source]
cast argument attribute as an
Argumentfor type checking framework/IDE.- Return type:
- neuralib.argp.core.with_defaults(instance)[source]
Initialize all argument attributes by assign the default value if provided.
- Parameters:
instance (T)
- Returns:
instance itself
- Return type:
T
- neuralib.argp.core.as_dict(instance)[source]
collect all argument attributes into a dictionary with attribute name to its value.
- Parameters:
instance (T)
- Returns:
- Return type:
dict[str, Any]
- neuralib.argp.core.copy_argument(opt, ref, **kwargs)[source]
copy argument from ref to opt
:param opt :param ref: :param kwargs: :return:
- Parameters:
opt (T)
- Return type:
T
- class neuralib.argp.core.Argument[source]
Descriptor (https://docs.python.org/3/glossary.html#term-descriptor). Carried the arguments pass to
argparse.ArgumentParser.add_argument.Creation
Use
argument().>>> class Example: ... a: str = argument('-a')
- __init__(*options, validator=None, validate_on_set=None, group=None, ex_group=None, hidden=False, **kwargs)[source]
- Parameters:
options – options
group (str | None) – argument group.
ex_group (str | None) – mutually exclusive group.
kwargs
validator (Callable[[T], bool] | None)
validate_on_set (bool | None)
hidden (bool)
- property default
- property const
- property metavar: str | None
- property choices: tuple[str, ...] | None
- property required: bool
- property help: str | None
- add_argument(ap, instance)[source]
Add this into argparse.ArgumentParser.
- Parameters:
ap (ArgumentParser)
instance
- Returns:
- with_options(option: str | dict[str, str] = None, *options: str, action: Literal['store', 'store_const', 'store_true', 'store_false', 'append', 'append_const', 'extend', 'count', 'help', 'version', 'boolean'] = None, nargs: int | Literal['*', '+', '?', '...'] = None, const: T = None, default: T = None, type: Type | Callable[[str], T] = None, validator: Callable[[T], bool] = None, validate_on_set: bool = None, choices: Sequence[str] = None, required: bool = None, hidden: bool = None, help: str = None, group: str = None, metavar: str = None) Argument[source]
Modify or update keyword parameter and return a new argument.
option flags update rule:
(): do not update options('-a', '-b'): replace options(..., '-c'): append options({'-a': '-A'}): rename options({'-a': '-A'}, ...): rename options, keep options if not in the dict.
general form:
() | (dict?, ...?, *str)- Parameters:
options – change option flags
kwargs – change keyword parameters, use … to unset parameter
- Returns: