A library and command-line tool for working with Boolean expressions
Project description
Synopsis
tt (truth table) is a library aiming to provide a Pythonic toolkit for working with Boolean expressions and truth tables. Please see the project site for guides and documentation, or check out bool.tools for a simple web application powered by this library.
Installation
tt is tested on CPython 3.6, 3.7, and 3.8. 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=1, C=1 A=1, B=0, C=1 A=1, B=1, C=1 A=0, B=0, C=0
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=1, C=1]>
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 Distributions
File details
Details for the file ttable-0.6.4.tar.gz
.
File metadata
- Download URL: ttable-0.6.4.tar.gz
- Upload date:
- Size: 122.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a39881ee3976ab4a188bc0560fa1f62c62c998b498667016f3124caae08c41ad |
|
MD5 | 8473b86066a5d39a25b3d917dcee6890 |
|
BLAKE2b-256 | 675c6d292f7ec2009cfd51ebf4f4a1b64eb537f2019498187b1272aa3722e43d |
File details
Details for the file ttable-0.6.4-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: ttable-0.6.4-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 81.9 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f91b4c3908f97edd1896d033767dc6d91ff4137a9bc1d1aff58d8581f0d5e1b2 |
|
MD5 | 4f3e026335934c6d3fae7223b11459a1 |
|
BLAKE2b-256 | 4d907a65689b13e292dbda1a792e1e79cda3cc24f6b9cc2a61aaa568885d51f3 |
File details
Details for the file ttable-0.6.4-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: ttable-0.6.4-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 81.9 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35740a8c96981503b8fc81483f5b372e580815cdb681881e9304de580300d6a7 |
|
MD5 | 94b1b4d1186db084a289aa148a16452f |
|
BLAKE2b-256 | a65efd33fc227f3975d85f9e3e33e6b4b4e152afdefda26d8f9de4030547756e |
File details
Details for the file ttable-0.6.4-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: ttable-0.6.4-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 81.9 kB
- Tags: CPython 3.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67bccf17ba96680e5c825d0cd41cd570b8ffd3974588f1514020e734019fff1a |
|
MD5 | c1ec6be4cb38386b55e12e6658a4ad70 |
|
BLAKE2b-256 | 39b59100904018655cb63cab7e2d48c5261207243fd92b8eb07baa0f85bebc57 |