Skip to main content

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 truth tables 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 than And and And has greater precedence than Or, Implies, Iff, and Xor.

  • T and F 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


Download files

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

Source Distribution

truth_tables-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

truth_tables-0.1.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file truth_tables-0.1.1.tar.gz.

File metadata

  • Download URL: truth_tables-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for truth_tables-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ee8c0a2ea5b8163b44c45442563f490a5f4e72d7898e95140307578e4e4afe87
MD5 df93878d384c56ba6079d96629af6a4b
BLAKE2b-256 fadf2cdd798230f38b1ca078f9ea1e0f0b351a90eba1fe2e0e84d1c9f98e7261

See more details on using hashes here.

File details

Details for the file truth_tables-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for truth_tables-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fcfe1e64a97f0c6b1fa890ea46d49396b062aac1e5bd9fc51a3b3633b3cff932
MD5 01e69f4a3acabfbf3efb6e45354d7a8b
BLAKE2b-256 cffc9effcb1b2b0e72785395b5670a9061450e2ad6eeb2a3bfe41ca6fd4bc457

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page