Skip to content

Instantly share code, notes, and snippets.

@oshinko
Last active November 10, 2025 03:37
Show Gist options
  • Select an option

  • Save oshinko/1984b25868e5428e3e707ac21466db32 to your computer and use it in GitHub Desktop.

Select an option

Save oshinko/1984b25868e5428e3e707ac21466db32 to your computer and use it in GitHub Desktop.
Convert to Boolean
def boolify(value: object) -> bool:
"""
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) and value.lower() in set(('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