tables

Tools for working with truth tables.

tables.truth_table module

Implementation of a truth table.

class tt.tables.truth_table.TruthTable(expr=None, from_values=None, fill_all=True, ordering=None)[source]

Bases: object

A class representing a truth table.

There are two ways to fill a table: either populated from an expression or by specifying the values yourself.

An existing BooleanExpression expression can be used, or you can just pass in a string:

>>> from tt import TruthTable
>>> t = TruthTable('A xor B')
>>> print(t)
+---+---+---+
| A | B |   |
+---+---+---+
| 0 | 0 | 0 |
+---+---+---+
| 0 | 1 | 1 |
+---+---+---+
| 1 | 0 | 1 |
+---+---+---+
| 1 | 1 | 0 |
+---+---+---+

When manually specifying the values tt can generate the symbols for you:

>>> from tt import TruthTable
>>> t = TruthTable(from_values='0110')
>>> print(t)
+---+---+---+
| A | B |   |
+---+---+---+
| 0 | 0 | 0 |
+---+---+---+
| 0 | 1 | 1 |
+---+---+---+
| 1 | 0 | 1 |
+---+---+---+
| 1 | 1 | 0 |
+---+---+---+

You can also specify the symbol names yourself, if you’d like:

>>> from tt import TruthTable
>>> t = TruthTable(from_values='0110', ordering=['tt', 'rocks'])
>>> print(t)
+----+-------+---+
| tt | rocks |   |
+----+-------+---+
| 0  |   0   | 0 |
+----+-------+---+
| 0  |   1   | 1 |
+----+-------+---+
| 1  |   0   | 1 |
+----+-------+---+
| 1  |   1   | 0 |
+----+-------+---+
Parameters:
  • expr (str or BooleanExpression) – The expression with which to populate this truth table. If this argument is omitted, then the from_values argument must be properly set.
  • from_values (str) – A string of 1’s, 0’s, and x’s representing the values to be stored in the table; the length of this string must be a power of 2 and is the complete set of values (in sequential order) to be stored in table.
  • fill_all (bool, optional) – A flag indicating whether the entirety of the table should be filled on initialization; defaults to True.
  • ordering (List[str], optional) – An input that maps to this class’s ordering property. If omitted, the ordering of symbols in the table will match that of the symbols’ appearance in the original expression.
Raises:
  • ConflictingArgumentsError – If both expr and from_values are specified in the initalization; a table can only be instantiated from one or the other.
  • DuplicateSymbolError – If multiple symbols of the same name are passed into the ordering list.
  • ExtraSymbolError – If a symbol not present in the expression is passed into the ordering list.
  • MissingSymbolError – If a symbol present in the expression is omitted from the ordering list.
  • InvalidArgumentTypeError – If an unexpected parameter type is encountered.
  • InvalidArgumentValueError – If the number of values specified via from_values is not a power of 2 or the ordering list (when filling the table using from_values) is empty.
  • InvalidIdentifierError – If any symbol names specified in ordering are not valid identifiers.
  • NoEvaluationVariationError – If an expression without any unqiue symbols (i.e., one merely composed of constant operands) is specified.
  • RequiredArgumentError – If neither the expr or from_values arguments are specified.
__init__(expr=None, from_values=None, fill_all=True, ordering=None)[source]

Initialize self. See help(type(self)) for accurate signature.

__str__()[source]

Return str(self).

equivalent_to(other)[source]

Return whether this table is equivalent to another source of truth.

Parameters:

other (TruthTable, str, or BooleanExpression) – The other source of truth with which to compare logical equivalence.

Returns:

True if the other expression is logically equivalent to this one, otherwise False.

Return type:

bool

Raises:

It is important to note that the concept of equivalence employed here is only concerned with the corresponding outputs between this table and the other provided source of truth. For example, the ordering of symbols is not taken into consideration when computing equivalence:

>>> from tt import TruthTable
>>> t1 = TruthTable('op1 or op2')
>>> t2 = TruthTable('A or B')
>>> t1.equivalent_to(t2)
True
>>> t2.equivalent_to(t1)
True

Another area of possible ambiguity here is the role of the don’t care value in equivalence. When comparing tables, don’t cares in the caller will allow for any corresponding value in other, but the reverse is not true. For example:

>>> from tt import TruthTable
>>> t1 = TruthTable(from_values='0x11')
>>> t2 = TruthTable(from_values='0011')
>>> t1.equivalent_to(t2)
True
>>> t2.equivalent_to(t1)
False

Additionally, only full tables are valid for equivalence checks. The appropriate error will be raised if you attempt to check the equivalence of partially filled tables:

>>> from tt import TruthTable
>>> t = TruthTable('A or B', fill_all=False)
>>> t.fill(A=0)
>>> try:
...     t.equivalent_to('A or B')
... except Exception as e:
...     print(type(e))
...
<class 'tt.errors.state.RequiresFullTableError'>
expr

The BooleanExpression object represented by this table.

This attribute will be None if this table was not derived from an expression (i.e., the user provided the values).

Type:BooleanExpression
fill(**kwargs)[source]

Fill the table with results, based on values specified by kwargs.

Parameters:

kwargs – Filter which entries in the table are filled by specifying symbol values through the keyword args.

Raises:

An example of iteratively filling a table:

>>> from tt import TruthTable
>>> t = TruthTable('A or B', fill_all=False)
>>> print(t)
Empty!
>>> t.fill(A=0)
>>> print(t)
+---+---+---+
| A | B |   |
+---+---+---+
| 0 | 0 | 0 |
+---+---+---+
| 0 | 1 | 1 |
+---+---+---+
>>> t.fill(A=1)
>>> print(t)
+---+---+---+
| A | B |   |
+---+---+---+
| 0 | 0 | 0 |
+---+---+---+
| 0 | 1 | 1 |
+---+---+---+
| 1 | 0 | 1 |
+---+---+---+
| 1 | 1 | 1 |
+---+---+---+
static generate_symbols(num_symbols)[source]

Generate a list of symbols for a specified number of symbols.

Generated symbol names are permutations of a properly-sized number of uppercase alphabet letters.

Parameters:num_symbols (int) – The number of symbols to generate.
Returns:A list of strings of length num_symbols, containing auto-generated symbols.
Return type:List[str]

A simple example:

>>> from tt import TruthTable
>>> TruthTable.generate_symbols(3)
['A', 'B', 'C']
>>> TruthTable.generate_symbols(7)
['A', 'B', 'C', 'D', 'E', 'F', 'G']
static input_combos(combo_len)[source]

Get an iterator of Boolean input combinations for this expression.

Parameters:combo_len (int, optional) – The length of each combination in the returned iterator.
Returns:An iterator of tuples containing permutations of Boolean inputs.
Return type:itertools.product

A simple example:

>>> from tt import TruthTable
>>> for tup in TruthTable.input_combos(2):
...     print(tup)
...
(False, False)
(False, True)
(True, False)
(True, True)
is_full

A Boolean flag indicating whether this table is full or not.

Type:bool

Attempting to further fill an already-full table will raise an AlreadyFullTableError:

>>> from tt import TruthTable
>>> t = TruthTable('A or B', fill_all=False)
>>> t.is_full
False
>>> t.fill()
>>> t.is_full
True
>>> try:
...     t.fill()
... except Exception as e:
...     print(type(e))
...
<class 'tt.errors.state.AlreadyFullTableError'>
ordering

The order in which the symbols should appear in the truth table.

Type:List[str]

Here’s a short example of alternative orderings of a partially-filled, three-symbol table:

>>> from tt import TruthTable
>>> t = TruthTable('(A or B) and C', fill_all=False)
>>> t.fill(A=0, B=0)
>>> print(t)
+---+---+---+---+
| A | B | C |   |
+---+---+---+---+
| 0 | 0 | 0 | 0 |
+---+---+---+---+
| 0 | 0 | 1 | 0 |
+---+---+---+---+
>>> t = TruthTable('(A or B) and C',
...                fill_all=False, ordering=['C', 'B', 'A'])
>>> t.fill(A=0, B=0)
>>> print(t)
+---+---+---+---+
| C | B | A |   |
+---+---+---+---+
| 0 | 0 | 0 | 0 |
+---+---+---+---+
| 1 | 0 | 0 | 0 |
+---+---+---+---+
results

A list containing the results of each possible set of inputs.

Type:List[bool, str]

In the case that the table is not completely filled, spots in this list that do not yet have a computed result will hold the None value.

Regardless of the filled status of this table, all positions in the results list are allocated at initialization and subsequently filled as computed. This is illustrated in the below example:

>>> from tt import TruthTable
>>> t = TruthTable('A or B', fill_all=False)
>>> t.results
[None, None, None, None]
>>> t.fill(A=0)
>>> t.results
[False, True, None, None]
>>> t.fill()
>>> t.results
[False, True, True, True]

If the table is filled upon initialization via the from_values parameter, don’t care strings could be present in the result list:

>>> from tt import TruthTable
>>> t = TruthTable(from_values='1xx0')
>>> t.results
[True, 'x', 'x', False]