Skip to content

Instantly share code, notes, and snippets.

@oshinko
Last active November 24, 2022 10:37
Show Gist options
  • Save oshinko/1984b25868e5428e3e707ac21466db32 to your computer and use it in GitHub Desktop.
Save oshinko/1984b25868e5428e3e707ac21466db32 to your computer and use it in GitHub Desktop.
Convert to Boolean
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