Registry / serialization / boolean-py

boolean-py

JSON →
library5.0pypypi✓ verified 25d ago

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-py
INSTALL
IMPORT
SIG · BOOLEAN-PY
B
boolean-py
serializationpythonv5.0
Install
1.5s avg
Import
26ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.026s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.026s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BooleanAlgebra
from boolean.boolean import BooleanAlgebra
For creating an algebra instance and defining symbols/expressions.
boolean
import boolean
Commonly used to access the top-level package, often followed by `boolean.BooleanAlgebra()`.

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.

import boolean algebra = boolean.BooleanAlgebra() # Define symbols fever = algebra.Symbol('fever') cough = algebra.Symbol('cough') headache = algebra.Symbol('headache') # Create expressions expression_string = "(fever | cough) & ~headache" parsed_expression = algebra.parse(expression_string) print(f"Parsed expression: {parsed_expression}") # Evaluate the expression for specific symptoms patient_symptoms = { 'fever': True, 'cough': False, 'headache': False } evaluated_expression = parsed_expression.subs(patient_symptoms) simplified_result = evaluated_expression.simplify() print(f"Evaluated result for symptoms: {simplified_result}") # Direct Python expression construction x, y, z = algebra.symbols('x', 'y', 'z') expr2 = (x & y) | (~z) print(f"Python-constructed expression: {expr2}") print(f"Simplified expr2: {expr2.simplify()}")
Debug
Known issues
gotchaBoolean expressions created with `boolean-py` are not automatically evaluated or simplified upon construction. Users must explicitly call the `.simplify()` method on an expression to apply Boolean algebra laws and obtain its simplified form.
fix
Always call `.simplify()` on expressions when you need the evaluated or canonical form. E.g., `expression.simplify()`.
affects: All versions
gotchaThe library natively provides `AND`, `OR`, and `NOT` as core operators. More complex boolean functions like `XOR` and `NAND` are not implemented directly and must be constructed by users through combinations of the basic operators.
fix
For `XOR`, use `(A & ~B) | (~A & B)`. For `NAND`, use `~ (A & B)`.
affects: All versions
gotchaWhile `boolean-py` overloads the `~` operator for its `Symbol` instances (e.g., `~algebra.Symbol('x')` results in `NOT(Symbol('x'))`), users should be careful not to confuse this with Python's built-in `~` operator's behavior on standard `bool` values. In Python, `~True` evaluates to -2 and `~False` to -1, as `bool` is a subclass of `int`. This can be a source of confusion when transitioning between `boolean-py` expressions and standard Python boolean logic.
fix
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'boolean'
The library is installed as `boolean-py` but is often imported using `import boolean` or `from boolean import ...`, which requires the package name to be 'boolean'. If `pip install boolean` was used, it would install a different, older package. The actual package name for import is 'boolean'.
fix
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.
boolean.ParseError: Unknown token
This error occurs when the string provided to `BooleanAlgebra().parse()` contains characters or sequences that are not recognized as valid boolean operators, symbols, or parentheses by the library's tokenizer and parser.
fix
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)')`).
AttributeError: 'bool' object has no attribute '...' (e.g., 'evaluate', 'simplify')
This typically happens when a native Python boolean value (`True` or `False`) is mistakenly treated as an object from the `boolean-py` library (like `Symbol` or an `Expression` object), and a method specific to the library's objects is called on it.
fix
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`.
SyntaxError: invalid syntax (when defining an expression string)
While `boolean-py` parses expressions from strings, this Python `SyntaxError` might occur if the expression string itself is not correctly enclosed or if Python's reserved keywords (`and`, `or`, `not`) are used without proper quotation or capitalization in the expression string expected by `boolean-py`'s parser, leading to a Python syntax error before the `boolean-py` parser even gets to it. It can also occur if Python code interacting with the library has basic syntax errors (e.g., `BooleanAlgebra().parse(A and B)` instead of `BooleanAlgebra().parse('A AND B')`).
fix
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.
Upgrade
Version history
5.0latest on PyPI · released Apr 3, 2025
Audit
Dependencies
PythonrequiredRequires Python 3.6 or higher.
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
boolean-py — pip install boolean-py · libregistry