tables¶
Tools for working with truth tables.
tables.truth_table module¶
Implementation of a truth table.
-
class
tt.tables.truth_table.TruthTable(expr, fill_all=True, ordering=None)[source]¶ Bases:
objectA class representing a truth table.
Parameters: - expr (
strorBooleanExpression) – The expression with which to populate this truth 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’sorderingproperty. If omitted, the ordering of symbols in the table will match that of the symbols’ appearance in the original expression.
Raises: - DuplicateSymbolError – If multiple symbols of the same name are
passed into the
orderinglist. - ExtraSymbolError – If a symbol not present in the expression is
passed into the
orderinglist. - MissingSymbolError – If a symbol present in the expression is
omitted from the
orderinglist. - InvalidArgumentTypeError – If an unexpected parameter type is encountered.
- NoEvaluationVariationError – If an expression without any unqiue symbols (i.e., one merely composed of constant operators) is specified.
Note
See
assert_iterable_contains_all_expr_symbolsfor more information about the exceptions raised by this class’s initializer.-
expr¶ The
BooleanExpressionobject represented by this table.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: - ExtraSymbolError – If a symbol not in the expression is passed as a keyword arg.
- InvalidBooleanValueError – If a non-Boolean value is passed as a value for one of the keyword args.
Note
See
assert_all_valid_keysfor more information about the exceptions raised by this method.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 | +---+---+---+
-
input_combos(combo_len=None)[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. If omitted, this defaults to the number of symbols in the expression.Returns: An iterator of tuples containing permutations of Boolean inputs. Return type: itertools.productThe length of each tuple of combinations is the same as the number of symbols in this expression if no
combo_lenvalue is specified; otherwise, the specified value is used.Iterating through the returned value, without fiddling with the
combo_leninput, will yield every combination of inputs for this expression.A simple example:
>>> from tt import TruthTable >>> t = TruthTable('A and B') >>> for tup in t.input_combos(): ... print(tup) ... (False, False) (False, True) (True, False) (True, True)
-
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]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
Nonevalue.Regardless of the filled status of this table, all positions in the
resultslist 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]
- expr (