edq.util.parse
1import typing 2 3BOOL_TRUE_STRINGS: typing.Set[str] = { 4 'true', 't', 5 'yes', 'y', 6 '1', 7} 8 9BOOL_FALSE_STRINGS: typing.Set[str] = { 10 'false', 'f', 11 'no', 'n', 12 '0', 13} 14 15def boolean(raw_text: typing.Union[str, bool]) -> bool: 16 """ 17 Parse a boolean from a string using common string representations for true/false. 18 This function assumes the entire string is the boolean (not just a part of it). 19 If the string is not true or false, then raise an exception. 20 """ 21 22 if (isinstance(raw_text, bool)): 23 return raw_text 24 25 text = str(raw_text).lower().strip() 26 27 if (text in BOOL_TRUE_STRINGS): 28 return True 29 30 if (text in BOOL_FALSE_STRINGS): 31 return False 32 33 raise ValueError(f"Could not convert text to boolean: '{raw_text}'.")
BOOL_TRUE_STRINGS: Set[str] =
{'1', 'true', 'y', 't', 'yes'}
BOOL_FALSE_STRINGS: Set[str] =
{'n', '0', 'false', 'f', 'no'}
def
boolean(raw_text: Union[str, bool]) -> bool:
16def boolean(raw_text: typing.Union[str, bool]) -> bool: 17 """ 18 Parse a boolean from a string using common string representations for true/false. 19 This function assumes the entire string is the boolean (not just a part of it). 20 If the string is not true or false, then raise an exception. 21 """ 22 23 if (isinstance(raw_text, bool)): 24 return raw_text 25 26 text = str(raw_text).lower().strip() 27 28 if (text in BOOL_TRUE_STRINGS): 29 return True 30 31 if (text in BOOL_FALSE_STRINGS): 32 return False 33 34 raise ValueError(f"Could not convert text to boolean: '{raw_text}'.")
Parse a boolean from a string using common string representations for true/false. This function assumes the entire string is the boolean (not just a part of it). If the string is not true or false, then raise an exception.