A library and command-line tool for working with Boolean expressions
Project description
Synopsis
tt is a library aiming to provide a Pythonic toolkit for working with Boolean expressions. Please check out the project site for more information.
Installation
tt is tested on CPython 2.7, 3.3, 3.4, 3.5, and 3.6. You can get the latest release from PyPI with:
pip install ttable
Features
tt lets you do a few things with your prized Boolean expressions. Let’s start by parsing one:
>>> from tt import BooleanExpression >>> b = BooleanExpression('A impl not (B nand C)') >>> b.tokens ['A', 'impl', 'not', '(', 'B', 'nand', 'C', ')'] >>> b.symbols ['A', 'B', 'C'] >>> print(b.tree) impl `----A `----not `----nand `----B `----C
Then transform it a couple of times:
>>> from tt import apply_de_morgans, to_cnf, to_primitives >>> b = to_primitives(b) >>> b <BooleanExpression "not A or not (not B or not C)"> >>> b = apply_de_morgans(b) >>> b <BooleanExpression "not A or (not not B and not not C)"> >>> b = to_cnf(b) >>> b <BooleanExpression "(not A or B) and (not A or C)">
Poke around its structure:
>>> b.is_cnf True >>> b.is_dnf False >>> for clause in b.iter_clauses(): ... print(clause) ... not A or B not A or C
Find all of its SAT solutions:
>>> for sat_solution in b.sat_all(): ... print(sat_solution) ... A=0, B=1, C=1 A=0, B=1, C=0 A=0, B=0, C=1 A=0, B=0, C=0 A=1, B=1, C=1
Or just find one:
>>> with b.constrain(A=1): ... b.sat_one() ... <BooleanValues [A=1, B=1, C=1]>
Turn it into a truth table:
>>> from tt import TruthTable >>> t = TruthTable(b) >>> print(t) +---+---+---+---+ | A | B | C | | +---+---+---+---+ | 0 | 0 | 0 | 1 | +---+---+---+---+ | 0 | 0 | 1 | 1 | +---+---+---+---+ | 0 | 1 | 0 | 1 | +---+---+---+---+ | 0 | 1 | 1 | 1 | +---+---+---+---+ | 1 | 0 | 0 | 0 | +---+---+---+---+ | 1 | 0 | 1 | 0 | +---+---+---+---+ | 1 | 1 | 0 | 0 | +---+---+---+---+ | 1 | 1 | 1 | 1 | +---+---+---+---+
And compare it to another truth table:
>>> other_table = TruthTable(from_values='111x00x1') >>> other_table.ordering ['A', 'B', 'C'] >>> for inputs, result in other_table: ... print(inputs, '=>', result) ... A=0, B=0, C=0 => True A=0, B=0, C=1 => True A=0, B=1, C=0 => True A=0, B=1, C=1 => x A=1, B=0, C=0 => False A=1, B=0, C=1 => False A=1, B=1, C=0 => x A=1, B=1, C=1 => True >>> other_table.equivalent_to(t) True
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
ttable-0.6.2.tar.gz
(699.7 kB
view hashes)
Built Distributions
ttable-0.6.2-cp36-cp36m-win32.whl
(69.2 kB
view hashes)
ttable-0.6.2-cp35-cp35m-win32.whl
(69.2 kB
view hashes)
ttable-0.6.2-cp34-cp34m-win32.whl
(73.4 kB
view hashes)
ttable-0.6.2-cp33-cp33m-win32.whl
(73.4 kB
view hashes)
ttable-0.6.2-cp27-cp27m-win32.whl
(71.9 kB
view hashes)
Close
Hashes for ttable-0.6.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 658d0c427dc60f79ccd956864ed78ca3ae9166bd1e2facef7e6b946ceb38c3cf |
|
MD5 | 2e7c4d53ca86cdcc13c5d8e258b297b6 |
|
BLAKE2b-256 | 51ebcaf00a3b177cd0e048c0433c7c80daaecfa22f021977da8f7d756f2cd6da |
Close
Hashes for ttable-0.6.2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7499da5ee1b8490abf802dee3ce01106ef0e66ffa86507b0b60e479166dfdf6c |
|
MD5 | bba937eae42bcf876724a74c7a42936d |
|
BLAKE2b-256 | eb6b8bff7e88dd897771861953d8ec4f715184cf2aeae47d12e7d2f09eef2234 |
Close
Hashes for ttable-0.6.2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02c7d574f0381da53aee8ff0cb222c4d4ecb41652561509b7bbceba0e3c7051e |
|
MD5 | d82c15418f0a33fb9366d41c729d5355 |
|
BLAKE2b-256 | 869e46bd16f5579570619ff73f8d583aac37cb61aaa1d0cf9716618331a3acd2 |
Close
Hashes for ttable-0.6.2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f5f6c7a7cd746e120faae3dc2552823080fc7a464b1bbf341ab1fdeaba80431 |
|
MD5 | 6ff8e9a0c72d58c272b345dc3997acd9 |
|
BLAKE2b-256 | 304778be31b3bd5ed3bee101bff4f6b0c3fd323944a1f2128bd6678c18c9ac9b |
Close
Hashes for ttable-0.6.2-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3698eee619e14ea3907f7fd9e18d473fc074220b07c3e0e9892b793eece40fc9 |
|
MD5 | d94e88e3e4b9d5e1fbd1e257d3f2a97a |
|
BLAKE2b-256 | dbe36147b1af941ef9672ea315a405b4bc5e96dfff84beec1c305586345a48ad |
Close
Hashes for ttable-0.6.2-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a3345311f1a56eb3be5821f72c1380181677358399ddbd29977e07e4bd070df |
|
MD5 | 61689da54301775c9aeb478d8ad79200 |
|
BLAKE2b-256 | 161bd2e00d8eef99206030a6c236dc798f850f700112f6812b6f04cb6ced5b75 |
Close
Hashes for ttable-0.6.2-cp33-cp33m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e078c0b52e2c9ef73f14e5e0ac2c3593e730cd60f301ab2bcf9708948aa59881 |
|
MD5 | cb6c2dec02bba660b62412bdacf69a49 |
|
BLAKE2b-256 | cc9a8ba2b2155d5ec136b6d8d1acbe36fd3c624b3aa7d2c4f8b28d5923f0857f |
Close
Hashes for ttable-0.6.2-cp33-cp33m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f8eee414d8bfea17c3f01369c8c15d76830ee74288525af861e11b2c21025dc |
|
MD5 | 1e4dc9e0430a1899b76f22926fe761bf |
|
BLAKE2b-256 | f5fc82b92511e720de3f85e4b512b3a56315ba97c404f97d6a399f7ac4f9f969 |
Close
Hashes for ttable-0.6.2-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa6ee62ce41ed1b91d8d2850298792eef7e4a06a7fc274f72602d2d044545a74 |
|
MD5 | 8f1c69839014fe4cc5a43e6918c04b9d |
|
BLAKE2b-256 | 42a5ea490bdb1f39ff06b8e122415b496669db8157189c845bd2fc84b9d0d1b0 |
Close
Hashes for ttable-0.6.2-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b710c61ef43ddb817049ecb55d1dff0cbf2bc6d609ff9be98cbdc23b819b1c9d |
|
MD5 | e422aa100409302349345da6a9aff0de |
|
BLAKE2b-256 | 63283ed1301fa9535bcad7c48e0e2e97c8ef6803081f4ea32d102ce2bb7b7b6b |