espnet2.utils.nested_dict_action.NestedDictAction
Less than 1 minute
espnet2.utils.nested_dict_action.NestedDictAction
class espnet2.utils.nested_dict_action.NestedDictAction(option_strings, dest, nargs=None, default=None, choices=None, required=False, help=None, metavar=None)
Bases: Action
Action class to append items to a dictionary object.
This class extends the argparse.Action to allow the user to pass configuration options in a nested dictionary format via command-line arguments. It supports both key-value pairs and YAML strings.
Examples
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('--conf', action=NestedDictAction,
... default={'a': 4})
>>> parser.parse_args(['--conf', 'a=3', '--conf', 'c=4'])
Namespace(conf={'a': 3, 'c': 4})
>>> parser.parse_args(['--conf', 'c.d=4'])
Namespace(conf={'a': 4, 'c': {'d': 4}})
>>> parser.parse_args(['--conf', 'c.d=4', '--conf', 'c=2'])
Namespace(conf={'a': 4, 'c': 2})
>>> parser.parse_args(['--conf', '{d: 5, e: 9}'])
Namespace(conf={'d': 5, 'e': 9})
_syntax
Syntax for using the action in command-line arguments.
Type: str
Parameters:
- option_strings (list) – The option strings for the action.
- dest (str) – The name of the attribute to be added to the namespace.
- nargs (int or str , optional) – The number of command-line arguments that should be consumed.
- default (dict , optional) – The default value for the destination.
- choices (list , optional) – The allowable choices for the argument.
- required (bool , optional) – Whether or not the action is required.
- help (str , optional) – The help text for the argument.
- metavar (str , optional) – A name for the argument in usage messages.
Raises:
- argparse.ArgumentTypeError – If the value cannot be interpreted as a dictionary.
- argparse.ArgumentError – If the value cannot be interpreted as a dictionary when using YAML.
NOTE
The values can be specified in the following formats: : - {key}=
- {key}.{subkey}=
- {key: value, …} (Python dict syntax)
- {key: value, …} (YAML syntax)