espnet2.utils.types.int_or_none
Less than 1 minute
espnet2.utils.types.int_or_none
espnet2.utils.types.int_or_none(value: str) → int | None
Convert a string to an integer or return None if the string is a null value.
This function checks if the input string represents a null value (“none”, “null”, or “nil”). If so, it returns None. Otherwise, it attempts to convert the string to an integer and returns the result.
- Parameters:value (str) – The input string to be converted to an integer.
- Returns: The converted integer if the string can be converted, or None if the string is a null value.
- Return type: Optional[int]
Examples
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('--foo', type=int_or_none)
>>> parser.parse_args(['--foo', '456'])
Namespace(foo=456)
>>> parser.parse_args(['--foo', 'none'])
Namespace(foo=None)
>>> parser.parse_args(['--foo', 'null'])
Namespace(foo=None)
>>> parser.parse_args(['--foo', 'nil'])
Namespace(foo=None)