Skip to content

set_none_str_to_none

Test whether a string matches ‘none’ in any case. Return None if it does. Otherwise, return the original value

Parameters:

Name Type Description Default
value (str, type(None), int, float)

the string to test, or a value already set to None.

required

Returns:

Type Description
(str, type(None), int, float)

str, type(None), int, float: the original value if it does not match ‘none’ in any case, or None if it does.

Raises:

Type Description
TypeError

if the value is not a string, None or base python numeric.

Source code in callingcardstools/Analysis/yeast/rank_response/set_none_str_to_none.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def set_none_str_to_none(
        value: (str, type(None), int, float)) -> (str, type(None), int, float):
    """
    Test whether a string matches 'none' in any case. Return None if it does.
    Otherwise, return the original value

    Args:
        value (str, type(None), int, float): the string to test, or a value
            already set to None.

    Returns:
        str, type(None), int, float: the original value if it does not
            match 'none' in any case, or None if it does.

    Raises:
        TypeError: if the value is not a string, None or base python numeric.
    """
    if not isinstance(value, (str, type(None), int, float)):
        raise TypeError("value must be a string, None or base python numeric")

    none_pattern = r'(?i)^none$'

    # if the value is a string
    if isinstance(value, str):
        # test whether it matches 'none' in any case and return None if it does
        if bool(re.match(none_pattern, value)):
            return None
        # else, return the original value
        else:
            return value
    # if the value is not a string, it must be None, so return it
    return value