Create pretty-printed truth tables from boolean expressions!
Project description
truth_tables
Create pretty-printed truth tables and abstract syntax trees from boolean expressions! Installation is as easy as pip install truth_tables
.
Example usage:
>>> from truth_tables import TruthTable
>>> my_table = TruthTable('p or q', '~p -> q', 'T and ~T')
>>> print(my_table)
┌───┬───┬────────┬─────────┬──────────┐
│ p │ q │ p or q │ ~p -> q │ T and ~T │
├───┼───┼────────┼─────────┼──────────┤
│ F │ F │ F │ F │ F │
│ F │ T │ T │ T │ F │
│ T │ F │ T │ T │ F │
│ T │ T │ T │ T │ F │
└───┴───┴────────┴─────────┴──────────┘
>>> print(my_table.ast)
Or
├─Variable('p')
╰─Variable('q')
Implies
├─Negate
│ ╰─Variable('p')
╰─Variable('q')
And
├─LiteralTrue
╰─Negate
╰─LiteralTrue
>>> my_table = TruthTable('~((p xor (q and ~r) or q) and ~(p <-> r))')
>>> print(my_table)
┌───┬───┬───┬───────────────────────────────────────────┐
│ p | q | r | ~((p xor (q and ~r) or q) and ~(p <-> r)) │
├───┼───┼───┼───────────────────────────────────────────┤
│ F | F | F | T │
│ F | F | T | T │
│ F | T | F | T │
│ F | T | T | F │
│ T | F | F | F │
│ T | F | T | T │
│ T | T | F | F │
│ T | T | T | T │
└───┴───┴───┴───────────────────────────────────────────┘
Two TruthTables
are equal if they have the same variables and the same truth values (not necessarily the same propositions).
>>> TruthTable('p -> q') == TruthTable('~p or q')
True
Notes on the Parser
-
The parser will accept symbolic or english names for boolean operators:
operator symbolic english Not
~
not
And
&
and
Or
|
or
Implies
->
implies
Iff
<->
iff
Xor
^
xor
-
Not
has greater precendence thanAnd
andAnd
has greater precedence thanOr
,Implies
,Iff
, andXor
. -
T
andF
are parsed as boolean literals. -
Other than the english operator names and boolean literals, variable names may be any sequence of word characters that don't start with a digit.
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
Hashes for truth_tables-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b2a4a4985f03e79b5bfc25620c5c94c320e6aa27d723890752adc1ab3dc5e21 |
|
MD5 | ddfbcd24e6575269224b43a850a66213 |
|
BLAKE2b-256 | 23502bc63f67b3a00de7d11cc92af5efd43cbcf333921f22b2c9ac286c0048d0 |