Convert a string value to its corresponding boolean value.
(s: str | bool)
| 206 | |
| 207 | |
| 208 | def str_to_bool(s: str | bool) -> bool: |
| 209 | """Convert a string value to its corresponding boolean value.""" |
| 210 | if isinstance(s, bool): |
| 211 | return s |
| 212 | elif not isinstance(s, str): |
| 213 | raise TypeError("argument must be a string") |
| 214 | |
| 215 | true_values = ("true", "on", "1") |
| 216 | false_values = ("false", "off", "0") |
| 217 | |
| 218 | if s.lower() in true_values: |
| 219 | return True |
| 220 | elif s.lower() in false_values: |
| 221 | return False |
| 222 | else: |
| 223 | raise ValueError(f'not a recognized boolean value: {s}') |
| 224 | |
| 225 | |
| 226 | def strip_matching_quotes(s: str) -> str: |
no outgoing calls