Python toolkit for Boolean expressions
Project description
Synopsis
tt (truth table) is a library aiming to provide a toolkit for working with Boolean expressions and truth tables. Please see the project site for guides and documentation.
Installation
tt is tested on the latest three major versions of CPython. You can get the latest release from PyPI with:
pip install ttable
Features
Parse expressions:
>>> from tt import BooleanExpression
>>> b = BooleanExpression('A impl not (B nand C)')
>>> b.tokens
['A', 'impl', 'not', '(', 'B', 'nand', 'C', ')']
>>> print(b.tree)
impl
`----A
`----not
`----nand
`----B
`----C
Evaluate expressions:
>>> b = BooleanExpression('(A /\\ B) -> (C \\/ D)')
>>> b.evaluate(A=1, B=1, C=0, D=0)
False
>>> b.evaluate(A=1, B=1, C=1, D=0)
True
Interact with expression structure:
>>> b = BooleanExpression('(A and ~B and C) or (~C and D) or E')
>>> b.is_dnf
True
>>> for clause in b.iter_dnf_clauses():
... print(clause)
...
A and ~B and C
~C and D
E
Apply expression transformations:
>>> from tt import to_primitives, to_cnf
>>> to_primitives('A xor B')
<BooleanExpression "(A and not B) or (not A and B)">
>>> to_cnf('(A nand B) impl (C or D)')
<BooleanExpression "(A or C or D) and (B or C or D)">
Or create your own:
>>> from tt import tt_compose, apply_de_morgans, coalesce_negations, twice
>>> b = BooleanExpression('not (not (A or B))')
>>> f = tt_compose(apply_de_morgans, twice)
>>> f(b)
<BooleanExpression "not not A or not not B">
>>> g = tt_compose(f, coalesce_negations)
>>> g(b)
<BooleanExpression "A or B">
Exhaust SAT solutions:
>>> b = BooleanExpression('~(A or B) xor C')
>>> for sat_solution in b.sat_all():
... print(sat_solution)
...
A=0, B=0, C=0
A=1, B=0, C=1
A=0, B=1, C=1
A=1, B=1, C=1
Find just a few:
>>> with b.constrain(A=1): ... for sat_solution in b.sat_all(): ... print(sat_solution) ... A=1, B=0, C=1 A=1, B=1, C=1
Or just one:
>>> b.sat_one() <BooleanValues [A=0, B=0, C=0]>
Build truth tables:
>>> from tt import TruthTable
>>> t = TruthTable('A iff B')
>>> print(t)
+---+---+---+
| A | B | |
+---+---+---+
| 0 | 0 | 1 |
+---+---+---+
| 0 | 1 | 0 |
+---+---+---+
| 1 | 0 | 0 |
+---+---+---+
| 1 | 1 | 1 |
+---+---+---+
And much more!
License
tt uses the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ttable-0.7.0.tar.gz.
File metadata
- Download URL: ttable-0.7.0.tar.gz
- Upload date:
- Size: 38.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
824e0f03a061a37f50be526b2aac2cc26e65ebea1c617e6bc1894aabfea77559
|
|
| MD5 |
1e7a276eb4c4f9fd0c2b010a6a766401
|
|
| BLAKE2b-256 |
73c3cd5003a06cea2240182dc9cbb8a208abefe9fc8ea0fb66d8742a5ab3a81e
|
File details
Details for the file ttable-0.7.0-py3-none-any.whl.
File metadata
- Download URL: ttable-0.7.0-py3-none-any.whl
- Upload date:
- Size: 45.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14bdffa4ba5b544a84e9906e151e2341faffa71bd03fdd8a3175cea175c06738
|
|
| MD5 |
4cf574107938ac46e8478f8f96691319
|
|
| BLAKE2b-256 |
7a8e7453510c1ebb1cf163182b0ce041a56ec6628b5e60646eff5886f51b5956
|