Skip to main content

A library and command-line tool for working with Boolean expressions

Project description

tt's PyPI page Linux build on Travis CI Windows build on AppVeyor

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


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)

Uploaded source

Built Distributions

ttable-0.6.2-cp36-cp36m-win_amd64.whl (75.1 kB view hashes)

Uploaded cp36

ttable-0.6.2-cp36-cp36m-win32.whl (69.2 kB view hashes)

Uploaded cp36

ttable-0.6.2-cp35-cp35m-win_amd64.whl (75.1 kB view hashes)

Uploaded cp35

ttable-0.6.2-cp35-cp35m-win32.whl (69.2 kB view hashes)

Uploaded cp35

ttable-0.6.2-cp34-cp34m-win_amd64.whl (79.9 kB view hashes)

Uploaded cp34

ttable-0.6.2-cp34-cp34m-win32.whl (73.4 kB view hashes)

Uploaded cp34

ttable-0.6.2-cp33-cp33m-win_amd64.whl (79.9 kB view hashes)

Uploaded cp33

ttable-0.6.2-cp33-cp33m-win32.whl (73.4 kB view hashes)

Uploaded cp33

ttable-0.6.2-cp27-cp27m-win_amd64.whl (78.5 kB view hashes)

Uploaded cp27

ttable-0.6.2-cp27-cp27m-win32.whl (71.9 kB view hashes)

Uploaded cp27

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page