Last active
November 24, 2022 10:37
-
-
Save oshinko/1984b25868e5428e3e707ac21466db32 to your computer and use it in GitHub Desktop.
Convert to Boolean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def boolify(value): | |
""" | |
Convert to bool. | |
>>> falsy = None, False, 0, b'', '', '0', 'false', 'off' | |
>>> truthy = True, 1, b'\\0', '1', 'true', 'on', 'A' | |
>>> any(boolify(x) for x in falsy) | |
False | |
>>> all(boolify(x) for x in truthy) | |
True | |
""" | |
if isinstance(value, str): | |
value = value.lower() | |
if value in ('0', 'false', 'off'): | |
return False | |
return bool(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment