espnet2.utils.types.str_or_none
Less than 1 minute
espnet2.utils.types.str_or_none
espnet2.utils.types.str_or_none(value: str) → str | None
Convert a string to None if it represents a null value.
This function checks if the input string is one of the specified null representations (‘none’, ‘null’, ‘nil’) and returns None if it is. Otherwise, it returns the original string.
- Parameters:value (str) – The input string to check and potentially convert.
- Returns: Returns None if the input string represents a null value; otherwise, returns the original string.
- Return type: Optional[str]
Examples
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('--foo', type=str_or_none)
>>> parser.parse_args(['--foo', 'aaa'])
Namespace(foo='aaa')
>>> parser.parse_args(['--foo', 'none'])
Namespace(foo=None)
>>> parser.parse_args(['--foo', 'null'])
Namespace(foo=None)
>>> parser.parse_args(['--foo', 'nil'])
Namespace(foo=None)