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.
Metadata
Release files for tobool 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tobool-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Release files / tobool-0.5.0-py3-none-any.whl
| Download URL | tobool-0.5.0-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5ac828985443c69f4bd0ea246c384def614c1e5228563887c27c56b9ca884e29
|
|
BLAKE2b-256 checksum How to use checksums |
ba96336aac52a0da327d74205246e57f47c65753f02d13bebddb88ef7bb15d35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.3
|