espnet2.utils.types.float_or_none
Less than 1 minute
espnet2.utils.types.float_or_none
espnet2.utils.types.float_or_none(value: str) → float | None
Convert a string to a float or return None.
This function attempts to convert a given string value to a float. If the string is one of “none”, “null”, or “nil” (case insensitive), it returns None. Otherwise, it converts the string to a float.
- Parameters:value (str) – The string value to convert.
- Returns: The converted float value, or None if the input string is “none”, “null”, or “nil”.
- Return type: Optional[float]
Examples
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('--foo', type=float_or_none)
>>> parser.parse_args(['--foo', '4.5'])
Namespace(foo=4.5)
>>> parser.parse_args(['--foo', 'none'])
Namespace(foo=None)
>>> parser.parse_args(['--foo', 'null'])
Namespace(foo=None)
>>> parser.parse_args(['--foo', 'nil'])
Namespace(foo=None)