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 "xor">, <BooleanOperator "or">, <BooleanOperator "nor">, <BooleanOperator "and">, <BooleanOperator "xnor">, <BooleanOperator "nand">, <BooleanOperator "impl">}

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 "nor">, <BooleanOperator "xor">, <BooleanOperator "xnor">, <BooleanOperator "nand">, <BooleanOperator "impl">}

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 = {'&&': <BooleanOperator "and">, '<->': <BooleanOperator "xnor">, 'XOR': <BooleanOperator "xor">, 'nand': <BooleanOperator "nand">, '\\/': <BooleanOperator "or">, 'AND': <BooleanOperator "and">, 'xnor': <BooleanOperator "xnor">, '~': <BooleanOperator "not">, 'iff': <BooleanOperator "xnor">, 'NOR': <BooleanOperator "nor">, 'IFF': <BooleanOperator "xnor">, '/\\': <BooleanOperator "and">, '||': <BooleanOperator "or">, 'nor': <BooleanOperator "nor">, 'OR': <BooleanOperator "or">, 'nxor': <BooleanOperator "xnor">, '!': <BooleanOperator "not">, 'or': <BooleanOperator "or">, 'IMPL': <BooleanOperator "impl">, 'xor': <BooleanOperator "xor">, 'NOT': <BooleanOperator "not">, '|': <BooleanOperator "or">, 'impl': <BooleanOperator "impl">, 'NAND': <BooleanOperator "nand">, 'NXOR': <BooleanOperator "xnor">, 'XNOR': <BooleanOperator "xnor">, '->': <BooleanOperator "impl">, 'and': <BooleanOperator "and">, 'not': <BooleanOperator "not">, '&': <BooleanOperator "and">}

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 = {'nor': <BooleanOperator "nor">, 'OR': <BooleanOperator "or">, 'nxor': <BooleanOperator "xnor">, 'nand': <BooleanOperator "nand">, 'XOR': <BooleanOperator "xor">, 'or': <BooleanOperator "or">, 'IMPL': <BooleanOperator "impl">, 'xor': <BooleanOperator "xor">, 'IFF': <BooleanOperator "xnor">, 'impl': <BooleanOperator "impl">, 'AND': <BooleanOperator "and">, 'NAND': <BooleanOperator "nand">, 'xnor': <BooleanOperator "xnor">, 'NXOR': <BooleanOperator "xnor">, 'XNOR': <BooleanOperator "xnor">, 'iff': <BooleanOperator "xnor">, 'NOR': <BooleanOperator "nor">, 'not': <BooleanOperator "not">, 'NOT': <BooleanOperator "not">, '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 "not">, '<->': <BooleanOperator "xnor">, '/\\': <BooleanOperator "and">, '!': <BooleanOperator "not">, '\\/': <BooleanOperator "or">, '|': <BooleanOperator "or">, '&&': <BooleanOperator "and">, '->': <BooleanOperator "impl">, '||': <BooleanOperator "or">, '&': <BooleanOperator "and">}

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