The boolean.py library implements a boolean algebra, allowing users to define, create, and parse boolean expressions with variables and core boolean functions (AND, OR, NOT). It supports parsing expressions from strings, simplifying them, and comparing for equivalence. Additionally, it provides capabilities for creating custom boolean DSLs by extending its tokenizer and parser. The library is currently active, with its latest version being 5.0, and generally releases updates as needed for compatibility and features.
pip install boolean-pyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize a `BooleanAlgebra` instance, define symbols, parse boolean expressions from strings, and construct them directly using Python operators. It also shows how to evaluate expressions by substituting symbol values and then simplifying them.
Always call `.simplify()` on expressions when you need the evaluated or canonical form. E.g., `expression.simplify()`.
For `XOR`, use `(A & ~B) | (~A & B)`. For `NAND`, use `~ (A & B)`.
Be mindful of the context: use `boolean-py`'s `NOT` (or `~` on `Symbol` instances) for logical negation within the algebra, and standard `not` for Python's built-in booleans. Avoid using `~` on standard `True`/`False` if you intend logical negation.
Ensure the library is installed using `pip install boolean-py` and then import it correctly using `from boolean import BooleanAlgebra, Symbol` or `import boolean_py as boolean_algebra` if ambiguity is a concern.
Review the input string for typos, unescaped special characters, or unsupported syntax. Ensure all variables are valid `Symbol` names and operators conform to 'AND', 'OR', 'NOT', and parentheses (e.g., `BooleanAlgebra().parse('A AND B (C)')` vs. `BooleanAlgebra().parse('A AND B OR (C)')`).Verify that the object you are calling methods on is indeed a `boolean-py` expression or symbol object, and not a standard Python boolean. This might involve checking the return type of a function or ensuring proper construction of boolean expressions within the library's context rather than letting them implicitly resolve to `bool`.
Always enclose boolean expressions meant for `boolean-py`'s `parse` method in quotes to pass them as strings (e.g., `algebra.parse('A AND B')`). Ensure correct Python syntax when writing code that defines or passes these strings. For the library's parser, use uppercase 'AND', 'OR', 'NOT' for operators in the string.