Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

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 2.7, 3.3, 3.4, 3.5, and 3.6. 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

Transform expressions:

>>> 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)">

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

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.

Release History

Release History

This version
History Node

0.6.3

History Node

0.6.2

History Node

0.6.1

History Node

0.6.0

History Node

0.5.1

History Node

0.5.0

History Node

0.4.1

History Node

0.3

History Node

0.2

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
ttable-0.6.3-cp27-cp27m-win32.whl (75.5 kB) Copy SHA256 Checksum SHA256 cp27 Wheel Nov 3, 2017
ttable-0.6.3-cp27-cp27m-win_amd64.whl (82.0 kB) Copy SHA256 Checksum SHA256 cp27 Wheel Nov 3, 2017
ttable-0.6.3-cp33-cp33m-win32.whl (77.0 kB) Copy SHA256 Checksum SHA256 cp33 Wheel Nov 3, 2017
ttable-0.6.3-cp33-cp33m-win_amd64.whl (83.5 kB) Copy SHA256 Checksum SHA256 cp33 Wheel Nov 3, 2017
ttable-0.6.3-cp34-cp34m-win32.whl (77.0 kB) Copy SHA256 Checksum SHA256 cp34 Wheel Nov 3, 2017
ttable-0.6.3-cp34-cp34m-win_amd64.whl (83.5 kB) Copy SHA256 Checksum SHA256 cp34 Wheel Nov 3, 2017
ttable-0.6.3-cp35-cp35m-win32.whl (72.8 kB) Copy SHA256 Checksum SHA256 cp35 Wheel Nov 3, 2017
ttable-0.6.3-cp35-cp35m-win_amd64.whl (78.6 kB) Copy SHA256 Checksum SHA256 cp35 Wheel Nov 3, 2017
ttable-0.6.3-cp36-cp36m-win32.whl (72.8 kB) Copy SHA256 Checksum SHA256 cp36 Wheel Nov 3, 2017
ttable-0.6.3-cp36-cp36m-win_amd64.whl (78.6 kB) Copy SHA256 Checksum SHA256 cp36 Wheel Nov 3, 2017
ttable-0.6.3.tar.gz (791.7 kB) Copy SHA256 Checksum SHA256 Source Nov 3, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting