espnet2.utils.types.str_or_int
Less than 1 minute
espnet2.utils.types.str_or_int
espnet2.utils.types.str_or_int(value: str) → str | int
Converts a string to an integer if possible; otherwise, returns the string.
This function attempts to convert the input string value to an integer. If the conversion is successful, the integer is returned. If a ValueError is raised during the conversion, the original string is returned instead.
- Parameters:value (str) – The string to convert to an integer.
- Returns: The converted integer if successful, otherwise the original string.
- Return type: Union[str, int]
Examples
>>> str_or_int("123")
123
>>> str_or_int("abc")
'abc'
>>> str_or_int("456")
456
>>> str_or_int("789.0")
'789.0'