Skip to main content

tobool

Convert many different representations of a bool to an actual bool or a None

Usage

pip install tobool
from tobool import to_bool

# Examples:
print(to_bool("True"))  # True
print(to_bool("False"))  # False
print(to_bool("Yes"))  # True
print(to_bool("T"))  # True
print(to_bool(1))  # True
print(to_bool(1.0))  # True
print(to_bool("1.0"))  # True
print(to_bool("enabled"))  # True
print(to_bool(b"true"))  # True
print(to_bool("None"))  # None
print(to_bool("n/a"))  # None
print(to_bool(float("nan")))  # None
from tobool import to_bool_strict

# to_bool_strict() will only return a bool
print(to_bool_strict("True"))  # True
print(to_bool_strict(None))  # False
print(to_bool_strict(None, True))  # tobool.tobool.ToBoolNoneValueException: None

Performance

Conversion results are cached via functools.lru_cache, so repeated conversions of the same value cost a single dict lookup. Measured on Python 3.14, a cache hit takes roughly 30 ns, versus roughly 140-720 ns to compute the conversion uncached (3x-22x faster, depending on the input type). Typical workloads such as parsing configuration values, environment variables, or CSV columns convert the same small set of values over and over, which is the case that benefits most.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tobool-0.5.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file tobool-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: tobool-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.3

File hashes

Hashes for tobool-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ac828985443c69f4bd0ea246c384def614c1e5228563887c27c56b9ca884e29
MD5 e96c3a25ebd27c8984282bbdfe93d812
BLAKE2b-256 ba96336aac52a0da327d74205246e57f47c65753f02d13bebddb88ef7bb15d35

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page