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.
- 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') >>> dict(instance._asdict()) {'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_name
is not a string.InvalidArgumentValueError – If
identifier_name
is 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
definitions.operators
module¶
Definitions for tt’s built-in Boolean operators.
- tt.definitions.operators.BINARY_OPERATORS = {<BooleanOperator "and">, <BooleanOperator "impl">, <BooleanOperator "nand">, <BooleanOperator "nor">, <BooleanOperator "or">, <BooleanOperator "xnor">, <BooleanOperator "xor">}¶
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]¶
A thin wrapper around a Boolean operator.
- property default_plain_english_str¶
The default plain English string representation of this operator.
Unlike
default_symbol_str
, this attribute should never beNone
.- Type:
>>> 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
- property 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
orNone
>>> 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
- property eval_func¶
The evaluation function wrapped by this operator.
- Type:
>>> from tt.definitions import TT_XOR_OP >>> TT_XOR_OP.eval_func(0, 0) False >>> TT_XOR_OP.eval_func(True, False) True
- tt.definitions.operators.MAX_OPERATOR_STR_LEN = 4¶
The length of the longest operator from
OPERATOR_MAPPING
.- Type:
- tt.definitions.operators.NON_PRIMITIVE_OPERATORS = {<BooleanOperator "impl">, <BooleanOperator "nand">, <BooleanOperator "nor">, <BooleanOperator "xnor">, <BooleanOperator "xor">}¶
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 "not">, '&': <BooleanOperator "and">, '&&': <BooleanOperator "and">, '->': <BooleanOperator "impl">, '/\\': <BooleanOperator "and">, '<->': <BooleanOperator "xnor">, 'AND': <BooleanOperator "and">, 'IFF': <BooleanOperator "xnor">, 'IMPL': <BooleanOperator "impl">, 'NAND': <BooleanOperator "nand">, 'NOR': <BooleanOperator "nor">, 'NOT': <BooleanOperator "not">, 'NXOR': <BooleanOperator "xnor">, 'OR': <BooleanOperator "or">, 'XNOR': <BooleanOperator "xnor">, 'XOR': <BooleanOperator "xor">, '\\/': <BooleanOperator "or">, 'and': <BooleanOperator "and">, 'iff': <BooleanOperator "xnor">, 'impl': <BooleanOperator "impl">, 'nand': <BooleanOperator "nand">, 'nor': <BooleanOperator "nor">, 'not': <BooleanOperator "not">, 'nxor': <BooleanOperator "xnor">, 'or': <BooleanOperator "or">, 'xnor': <BooleanOperator "xnor">, 'xor': <BooleanOperator "xor">, '|': <BooleanOperator "or">, '||': <BooleanOperator "or">, '~': <BooleanOperator "not">}¶
A mapping of all available Boolean operators.
This dictionary is the concatentation of the
PLAIN_ENGLISH_OPERATOR_MAPPING
andSYMBOLIC_OPERATOR_MAPPING
dictionaries.- Type:
Dict{
str
:BooleanOperator
}
- tt.definitions.operators.PLAIN_ENGLISH_OPERATOR_MAPPING = {'AND': <BooleanOperator "and">, 'IFF': <BooleanOperator "xnor">, 'IMPL': <BooleanOperator "impl">, 'NAND': <BooleanOperator "nand">, 'NOR': <BooleanOperator "nor">, 'NOT': <BooleanOperator "not">, 'NXOR': <BooleanOperator "xnor">, 'OR': <BooleanOperator "or">, 'XNOR': <BooleanOperator "xnor">, 'XOR': <BooleanOperator "xor">, 'and': <BooleanOperator "and">, 'iff': <BooleanOperator "xnor">, 'impl': <BooleanOperator "impl">, 'nand': <BooleanOperator "nand">, 'nor': <BooleanOperator "nor">, 'not': <BooleanOperator "not">, 'nxor': <BooleanOperator "xnor">, 'or': <BooleanOperator "or">, 'xnor': <BooleanOperator "xnor">, 'xor': <BooleanOperator "xor">}¶
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 "and">, '&&': <BooleanOperator "and">, '->': <BooleanOperator "impl">, '/\\': <BooleanOperator "and">, '<->': <BooleanOperator "xnor">, '\\/': <BooleanOperator "or">, '|': <BooleanOperator "or">, '||': <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:
- tt.definitions.operators.TT_IMPL_OP = <BooleanOperator "impl">¶
tt’s operator implementation of a Boolean IMPLIES.
- Type:
- tt.definitions.operators.TT_NAND_OP = <BooleanOperator "nand">¶
tt’s operator implementation of a Boolean NAND.
- Type:
- tt.definitions.operators.TT_NOR_OP = <BooleanOperator "nor">¶
tt’s operator implementation of a Boolean NOR.
- Type:
- tt.definitions.operators.TT_NOT_OP = <BooleanOperator "not">¶
tt’s operator implementation of a Boolean NOT.
- Type:
- tt.definitions.operators.TT_OR_OP = <BooleanOperator "or">¶
tt’s operator implementation of a Boolean OR.
- Type:
- tt.definitions.operators.TT_XNOR_OP = <BooleanOperator "xnor">¶
tt’s operator implementation of a Boolean XNOR.
- Type:
- tt.definitions.operators.TT_XOR_OP = <BooleanOperator "xor">¶
tt’s operator implementation of a Boolean XOR.
- Type: