str2none
- opthandler.str2none(s, case_sensitive=False, empty_none=False)[source]
Convert the string
'None'
to the NoneTypeNone
.If the input is
'None'
, convert it to the NoneTypeNone
, else raise aValueError
.- Parameters:
- Returns:
converted_string (
None
) – Returns the NoneTypeNone
if the input was'None'
or convertible to'None'
.- Raises:
ValueError – If the input string was not
'None'
.
See also
convert_str()
Convert a string to its corresponding type
Notes
This function was written as converter for a
ConfigParser
.Examples
>>> str2none(None) # Returns None >>> str2none('None') # Returns None >>> str2none('none') # Returns None >>> str2none('none', case_sensitive=True) Traceback (most recent call last): ... ValueError: Input cannot be convertet to NoneType >>> str2none('') Traceback (most recent call last): ... ValueError: Input cannot be convertet to NoneType >>> str2none('', empty_none=True) # Returns None >>> str2none(2) Traceback (most recent call last): ... ValueError: Input cannot be convertet to NoneType