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
orBooleanExpression
) – The expression with which to populate this truth table. If this argument is omitted, then thefrom_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 toTrue
.  ordering (List[
str
], optional) – An input that maps to this class’sordering
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
andfrom_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 theordering
list (when filling the table usingfrom_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
orfrom_values
arguments are specified.

equivalent_to
(other)[source]¶ Return whether this table is equivalent to another source of truth.
Parameters: other (
TruthTable
,str
, orBooleanExpression
) – 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: Raises:  InvalidArgumentTypeError – If the
other
argument is not one of the acceptable types.  RequiresFullTableError – If either the calling table or other source of truth represents an unfilled table.
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'>
 InvalidArgumentTypeError – If the

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:  AlreadyFullTableError – If the table is already full when this method is called.
 ExtraSymbolError – If a symbol not in the expression is passed as a keyword arg.
 InvalidBooleanValueError – If a nonBoolean value is passed as a value for one of the keyword args.
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 properlysized 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 autogenerated 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 alreadyfull 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 partiallyfilled, threesymbol 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]
 expr (