definitions

Definitions for tt’s expression grammar, operands, and operators.

definitions.grammar module

Definitions related to expression grammar.

tt.definitions.grammar.CONSTANT_VALUES = {'1', '0'}

Set of tokens that act as constant values in expressions.

Type:Set[str]
tt.definitions.grammar.DELIMITERS = {')', ' ', '('}

Set of tokens that act as delimiters in expressions.

Type:Set[str]

definitions.operands module

Definitions related to operands.

tt.definitions.operands.BOOLEAN_VALUES = {False, True}

Set of truthy values valid to submit for evaluation.

Type:Set[int, bool]
tt.definitions.operands.DONT_CARE_VALUE = 'x'

The don’t care string identifier.

Type:str
tt.definitions.operands.boolean_variables_factory(symbols)[source]

Returns a class for namedtuple-like objects for holding boolean values.

Parameters:symbols (List[str]) – A list of the symbol names for which instances of this class will hold an entry.
Returns:An object where the passed symbols can be accessed as attributes.
Return type:namedtuple-like object

This functionality is best demonstrated with an example:

>>> from tt import boolean_variables_factory
>>> factory = boolean_variables_factory(['op1', 'op2', 'op3'])
>>> instance = factory(op1=True, op2=False, op3=False)
>>> instance.op1
True
>>> instance.op2
False
>>> print(instance)
op1=1, op2=0, op3=0
>>> instance = factory(op1=0, op2=0, op3=1)
>>> instance.op3
1
>>> print(instance)
op1=0, op2=0, op3=1

It should be noted that this function is used internally within functionality where the validity of inputs is already checked. As such, this class won’t enforce the Boolean-ness of input values:

>>> factory = boolean_variables_factory(['A', 'B'])
>>> instance = factory(A=-1, B='value')
>>> print(instance)
A=-1, B=value

Instances produced from the generated factory are descendants of namedtuple generated classes; some of the inherited attributes may be useful:

>>> instance = factory(A=True, B=False)
>>> instance._fields
('A', 'B')
>>> instance._asdict()
OrderedDict([('A', True), ('B', False)])
tt.definitions.operands.is_valid_identifier(identifier_name)[source]

Returns whether the string is a valid symbol identifier.

Valid identifiers are those that follow Python variable naming conventions, are not Python keywords, and do not begin with an underscore.

Parameters:

identifier_name (str) – The string to test.

Returns:

True if the passed string is valid identifier, otherwise False.

Return type:

bool

Raises:

As an example:

>>> from tt import is_valid_identifier
>>> is_valid_identifier('$var')
False
>>> is_valid_identifier('va#r')
False
>>> is_valid_identifier('for')
False
>>> is_valid_identifier('False')
False
>>> is_valid_identifier('var')
True
>>> is_valid_identifier('')
Traceback (most recent call last):
    ...
tt.errors.arguments.InvalidArgumentValueError: identifier_name cannot be empty
>>> is_valid_identifier(None)
Traceback (most recent call last):
    ...
tt.errors.arguments.InvalidArgumentTypeError: identifier_name must be a string

definitions.operators module

Definitions for tt’s built-in Boolean operators.

tt.definitions.operators.BINARY_OPERATORS = {<BooleanOperator "nand">, <BooleanOperator "impl">, <BooleanOperator "xor">, <BooleanOperator "or">, <BooleanOperator "nor">, <BooleanOperator "and">, <BooleanOperator "xnor">}

The set of all binary operators available in tt.

Type:Set{BooleanOperator}
class tt.definitions.operators.BooleanOperator(precedence, eval_func, default_symbol_str, default_plain_english_str)[source]

Bases: object

A thin wrapper around a Boolean operator.

default_plain_english_str

The default plain English string representation of this operator.

Unlike default_symbol_str, this attribute should never be None.

Type:str
>>> from tt.definitions import TT_AND_OP, TT_NAND_OP
>>> print(TT_AND_OP.default_plain_english_str)
and
>>> print(TT_NAND_OP.default_plain_english_str)
nand
default_symbol_str

The default symbolic string representation of this operator.

Some operators may not have a recognized symbol str, in which case this attribute will be None.

Type:str or None
>>> from tt.definitions import TT_AND_OP, TT_NAND_OP
>>> print(TT_AND_OP.default_symbol_str)
/\
>>> print(TT_NAND_OP.default_symbol_str)
None
eval_func

The evaluation function wrapped by this operator.

Type:Callable
>>> from tt.definitions import TT_XOR_OP
>>> TT_XOR_OP.eval_func(0, 0)
False
>>> TT_XOR_OP.eval_func(True, False)
True
precedence

Precedence of this operator, relative to other operators.

Type:int
>>> from tt.definitions import TT_AND_OP, TT_OR_OP
>>> TT_AND_OP.precedence > TT_OR_OP.precedence
True
tt.definitions.operators.MAX_OPERATOR_STR_LEN = 4

The length of the longest operator from OPERATOR_MAPPING.

Type:int
tt.definitions.operators.NON_PRIMITIVE_OPERATORS = {<BooleanOperator "nand">, <BooleanOperator "impl">, <BooleanOperator "nor">, <BooleanOperator "xor">, <BooleanOperator "xnor">}

The set of non-primitive operators available in tt.

This includes all binary operators other than AND and OR.

Type:Set{BooleanOperator}
tt.definitions.operators.OPERATOR_MAPPING = {'nand': <BooleanOperator "nand">, 'XOR': <BooleanOperator "xor">, '&&': <BooleanOperator "and">, 'XNOR': <BooleanOperator "xnor">, 'and': <BooleanOperator "and">, 'NOT': <BooleanOperator "not">, '|': <BooleanOperator "or">, 'xor': <BooleanOperator "xor">, 'IFF': <BooleanOperator "xnor">, '~': <BooleanOperator "not">, 'impl': <BooleanOperator "impl">, 'not': <BooleanOperator "not">, 'nxor': <BooleanOperator "xnor">, '&': <BooleanOperator "and">, 'IMPL': <BooleanOperator "impl">, 'or': <BooleanOperator "or">, '->': <BooleanOperator "impl">, 'NXOR': <BooleanOperator "xnor">, 'OR': <BooleanOperator "or">, 'xnor': <BooleanOperator "xnor">, 'NAND': <BooleanOperator "nand">, 'NOR': <BooleanOperator "nor">, '/\\': <BooleanOperator "and">, '<->': <BooleanOperator "xnor">, 'iff': <BooleanOperator "xnor">, '!': <BooleanOperator "not">, '||': <BooleanOperator "or">, '\\/': <BooleanOperator "or">, 'AND': <BooleanOperator "and">, 'nor': <BooleanOperator "nor">}

A mapping of all available Boolean operators.

This dictionary is the concatentation of the PLAIN_ENGLISH_OPERATOR_MAPPING and SYMBOLIC_OPERATOR_MAPPING dictionaries.

Type:Dict{str: BooleanOperator}
tt.definitions.operators.PLAIN_ENGLISH_OPERATOR_MAPPING = {'nand': <BooleanOperator "nand">, 'XNOR': <BooleanOperator "xnor">, 'XOR': <BooleanOperator "xor">, 'xnor': <BooleanOperator "xnor">, 'NXOR': <BooleanOperator "xnor">, 'and': <BooleanOperator "and">, 'NOT': <BooleanOperator "not">, 'OR': <BooleanOperator "or">, 'xor': <BooleanOperator "xor">, 'nor': <BooleanOperator "nor">, 'iff': <BooleanOperator "xnor">, 'IFF': <BooleanOperator "xnor">, 'impl': <BooleanOperator "impl">, 'not': <BooleanOperator "not">, 'nxor': <BooleanOperator "xnor">, 'NOR': <BooleanOperator "nor">, 'or': <BooleanOperator "or">, 'NAND': <BooleanOperator "nand">, 'IMPL': <BooleanOperator "impl">, 'AND': <BooleanOperator "and">}

A mapping of Boolean operators.

This mapping includes the plain-English variants of the available Boolean operators.

Type:Dict{str: BooleanOperator}
tt.definitions.operators.SYMBOLIC_OPERATOR_MAPPING = {'<->': <BooleanOperator "xnor">, '&&': <BooleanOperator "and">, '!': <BooleanOperator "not">, '||': <BooleanOperator "or">, '/\\': <BooleanOperator "and">, '->': <BooleanOperator "impl">, '\\/': <BooleanOperator "or">, '&': <BooleanOperator "and">, '|': <BooleanOperator "or">, '~': <BooleanOperator "not">}

A mapping of Boolean operators.

This mapping includes the symbolic variants of the available Boolean operators.

Type:Dict{str: BooleanOperator}
tt.definitions.operators.TT_AND_OP = <BooleanOperator "and">

tt’s operator implementation of a Boolean AND.

Type:BooleanOperator
tt.definitions.operators.TT_IMPL_OP = <BooleanOperator "impl">

tt’s operator implementation of a Boolean IMPLIES.

Type:BooleanOperator
tt.definitions.operators.TT_NAND_OP = <BooleanOperator "nand">

tt’s operator implementation of a Boolean NAND.

Type:BooleanOperator
tt.definitions.operators.TT_NOR_OP = <BooleanOperator "nor">

tt’s operator implementation of a Boolean NOR.

Type:BooleanOperator
tt.definitions.operators.TT_NOT_OP = <BooleanOperator "not">

tt’s operator implementation of a Boolean NOT.

Type:BooleanOperator
tt.definitions.operators.TT_OR_OP = <BooleanOperator "or">

tt’s operator implementation of a Boolean OR.

Type:BooleanOperator
tt.definitions.operators.TT_XNOR_OP = <BooleanOperator "xnor">

tt’s operator implementation of a Boolean XNOR.

Type:BooleanOperator
tt.definitions.operators.TT_XOR_OP = <BooleanOperator "xor">

tt’s operator implementation of a Boolean XOR.

Type:BooleanOperator