definitions¶
Definitions for tt’s expression grammar, operands, and operators.
definitions.grammar module¶
Definitions related to expression grammar.
definitions.operands module¶
Definitions related to operands.
-
tt.definitions.operands.BOOLEAN_VALUES= {0, 1}¶ Set of truthy values valid to submit for evaluation.
Type: Set[ int,bool]
-
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 symbolscan be accessed as attributes.Return type: namedtuple-like objectThis 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
namedtuplegenerated 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: Raises: - InvalidArgumentTypeError – If
identifier_nameis not a string. - InvalidArgumentValueError – If
identifier_nameis an empty string.
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
- InvalidArgumentTypeError – If
definitions.operators module¶
Definitions for tt’s built-in Boolean operators.
-
tt.definitions.operators.BINARY_OPERATORS= {<BooleanOperator "xor">, <BooleanOperator "xnor">, <BooleanOperator "and">, <BooleanOperator "nand">, <BooleanOperator "or">, <BooleanOperator "nor">, <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:
objectA 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 beNone.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: strorNone>>> 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
-
-
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 "xor">, <BooleanOperator "xnor">, <BooleanOperator "nand">, <BooleanOperator "nor">, <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= {'not': <BooleanOperator "not">, 'NOT': <BooleanOperator "not">, 'xor': <BooleanOperator "xor">, 'XOR': <BooleanOperator "xor">, 'impl': <BooleanOperator "impl">, 'IMPL': <BooleanOperator "impl">, 'iff': <BooleanOperator "xnor">, 'IFF': <BooleanOperator "xnor">, 'xnor': <BooleanOperator "xnor">, 'XNOR': <BooleanOperator "xnor">, 'nxor': <BooleanOperator "xnor">, 'NXOR': <BooleanOperator "xnor">, 'and': <BooleanOperator "and">, 'AND': <BooleanOperator "and">, 'nand': <BooleanOperator "nand">, 'NAND': <BooleanOperator "nand">, 'or': <BooleanOperator "or">, 'OR': <BooleanOperator "or">, 'nor': <BooleanOperator "nor">, 'NOR': <BooleanOperator "nor">, '~': <BooleanOperator "not">, '!': <BooleanOperator "not">, '->': <BooleanOperator "impl">, '<->': <BooleanOperator "xnor">, '&&': <BooleanOperator "and">, '&': <BooleanOperator "and">, '/\\': <BooleanOperator "and">, '||': <BooleanOperator "or">, '|': <BooleanOperator "or">, '\\/': <BooleanOperator "or">}¶ A mapping of all available Boolean operators.
This dictionary is the concatentation of the
PLAIN_ENGLISH_OPERATOR_MAPPINGandSYMBOLIC_OPERATOR_MAPPINGdictionaries.Type: Dict{ str:BooleanOperator}
-
tt.definitions.operators.PLAIN_ENGLISH_OPERATOR_MAPPING= {'not': <BooleanOperator "not">, 'NOT': <BooleanOperator "not">, 'xor': <BooleanOperator "xor">, 'XOR': <BooleanOperator "xor">, 'impl': <BooleanOperator "impl">, 'IMPL': <BooleanOperator "impl">, 'iff': <BooleanOperator "xnor">, 'IFF': <BooleanOperator "xnor">, 'xnor': <BooleanOperator "xnor">, 'XNOR': <BooleanOperator "xnor">, 'nxor': <BooleanOperator "xnor">, 'NXOR': <BooleanOperator "xnor">, 'and': <BooleanOperator "and">, 'AND': <BooleanOperator "and">, 'nand': <BooleanOperator "nand">, 'NAND': <BooleanOperator "nand">, 'or': <BooleanOperator "or">, 'OR': <BooleanOperator "or">, 'nor': <BooleanOperator "nor">, 'NOR': <BooleanOperator "nor">}¶ 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 "not">, '->': <BooleanOperator "impl">, '<->': <BooleanOperator "xnor">, '&&': <BooleanOperator "and">, '&': <BooleanOperator "and">, '/\\': <BooleanOperator "and">, '||': <BooleanOperator "or">, '|': <BooleanOperator "or">, '\\/': <BooleanOperator "or">}¶ 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