{"library":"boolean-py","title":"boolean-py","description":"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.","status":"active","version":"5.0","language":"en","source_language":"en","source_url":"https://github.com/bastikr/boolean.py","tags":["boolean","logic","algebra","expression","parser","dsl"],"install":[{"cmd":"pip install boolean-py","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Requires Python 3.6 or higher.","package":"Python","optional":false}],"imports":[{"note":"For creating an algebra instance and defining symbols/expressions.","symbol":"BooleanAlgebra","correct":"from boolean.boolean import BooleanAlgebra"},{"note":"Commonly used to access the top-level package, often followed by `boolean.BooleanAlgebra()`.","symbol":"boolean","correct":"import boolean"}],"quickstart":{"code":"import boolean\n\nalgebra = boolean.BooleanAlgebra()\n\n# Define symbols\nfever = algebra.Symbol('fever')\ncough = algebra.Symbol('cough')\nheadache = algebra.Symbol('headache')\n\n# Create expressions\nexpression_string = \"(fever | cough) & ~headache\"\nparsed_expression = algebra.parse(expression_string)\nprint(f\"Parsed expression: {parsed_expression}\")\n\n# Evaluate the expression for specific symptoms\npatient_symptoms = {\n    'fever': True,\n    'cough': False,\n    'headache': False\n}\n\nevaluated_expression = parsed_expression.subs(patient_symptoms)\nsimplified_result = evaluated_expression.simplify()\nprint(f\"Evaluated result for symptoms: {simplified_result}\")\n\n# Direct Python expression construction\nx, y, z = algebra.symbols('x', 'y', 'z')\nexpr2 = (x & y) | (~z)\nprint(f\"Python-constructed expression: {expr2}\")\nprint(f\"Simplified expr2: {expr2.simplify()}\")","lang":"python","description":"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."},"warnings":[{"fix":"Always call `.simplify()` on expressions when you need the evaluated or canonical form. E.g., `expression.simplify()`.","message":"Boolean 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For `XOR`, use `(A & ~B) | (~A & B)`. For `NAND`, use `~ (A & B)`.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"While `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-05T00:00:00.000Z","next_check":"2026-07-04T00:00:00.000Z"}