Metadata-Version: 2.4
Name: tobool
Version: 0.5.0
Summary: convert any boolean-like value such as string, int, etc. to an actual bool
Home-page: https://github.com/jamesabel/tobool
Download-URL: https://github.com/jamesabel/tobool
Author: abel
Author-email: j@abel.co
License: MIT License
Keywords: boolean
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: author
Dynamic: author-email
Dynamic: description
Dynamic: description-content-type
Dynamic: download-url
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: license-file
Dynamic: summary


<p align="center">
    <a href="https://github.com/jamesabel/tobool/actions/workflows/python-package.yml" alt="build">
        <img src="https://img.shields.io/github/actions/workflow/status/jamesabel/tobool/python-package.yml?branch=main" />
    </a>
    <a href="https://pypi.org/project/tobool/" alt="pypi">
        <img src="https://img.shields.io/pypi/v/tobool" />
    </a>
    <a href="https://pypi.org/project/tobool/" alt="downloads">
        <img src="https://img.shields.io/pypi/dm/tobool" />
    </a>
    <a href="https://github.com/jamesabel/tobool/blob/main/LICENSE" alt="license">
        <img src="https://img.shields.io/github/license/jamesabel/tobool" />
    </a>
</p>

# tobool

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

# Usage

```
pip install tobool
```

```python
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
```

```python
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.
