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.

class tt.definitions.operators.BooleanOperator(precedence, eval_func, name)[source]

Bases: object

A wrapper around a Boolean operator.

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
name

The human-readable name of this operator.

Type:str
>>> from tt.definitions import TT_NOT_OP, TT_XOR_OP
>>> TT_NOT_OP.name
'NOT'
>>> TT_XOR_OP.name
'XOR'
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.OPERATOR_MAPPING = {'NAND': <BooleanOperator NAND>, 'NOT': <BooleanOperator NOT>, 'nxor': <BooleanOperator XNOR>, 'xor': <BooleanOperator XOR>, '&': <BooleanOperator AND>, '|': <BooleanOperator OR>, 'nor': <BooleanOperator NOR>, 'OR': <BooleanOperator OR>, 'NXOR': <BooleanOperator XNOR>, 'NOR': <BooleanOperator NOR>, '&&': <BooleanOperator AND>, 'or': <BooleanOperator OR>, '\\/': <BooleanOperator OR>, '~': <BooleanOperator NOT>, '/\\': <BooleanOperator AND>, 'XNOR': <BooleanOperator XNOR>, 'and': <BooleanOperator AND>, '!': <BooleanOperator NOT>, 'nand': <BooleanOperator NAND>, 'AND': <BooleanOperator AND>, 'XOR': <BooleanOperator XOR>, 'xnor': <BooleanOperator XNOR>, '||': <BooleanOperator OR>, 'not': <BooleanOperator NOT>}

A mapping of Boolean operators.

This mapping serves to define all valid operator strings and maps them to the appropriate BooleanOperator object defining the operator behavior.

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_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