str2none

opthandler.str2none(s, case_sensitive=False, empty_none=False)[source]

Convert the string 'None' to the NoneType None.

If the input is 'None', convert it to the NoneType None, else raise a ValueError.

Parameters:
  • s (str_like) – The input. Can be anything that can be converted to a string.

  • case_sensitive (bool, optional) – If False, also convert the lower case string 'none' to the NoneType None.

  • empty_none (bool, optional) – If True, also convert the empty string '' to the NoneType None.

Returns:

converted_string (None) – Returns the NoneType None 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