trees
¶
Tools for working with Boolean expression trees.
trees.expr_tree
module¶
An expression tree implementation for Boolean expressions.
-
class
tt.trees.expr_tree.
BooleanExpressionTree
(postfix_tokens)[source]¶ Bases:
object
An expression tree for Boolean expressions.
This class expects any input it receives to be well-formed; any tokenized lists you pass it directly (instead of from the attribute of the
BooleanExpression
class) will not be checked.-
evaluate
(input_dict)[source]¶ Evaluate the expression held in this tree for specified inputs.
Parameters: input_dict (Dict{ str
: truthy}) – A dict mapping string variable names to the values for which they should be evaluated.Returns: The result of the expression tree evaluation. Return type: bool
Note
This function does not check to ensure the validity of the
input_dict
argument in any way.While you would normally evaluate expressions through the interface provided by the
BooleanExpression
class, this interface is still exposed for your use if you want to avoid any overhead introduced by the extra layer of abstraction. For example:>>> from tt import BooleanExpressionTree >>> bet = BooleanExpressionTree(['A', 'B', 'xor']) >>> bet.evaluate({'A': 1, 'B': 0}) True >>> bet.evaluate({'A': 1, 'B': 1}) False
-
root
¶ The root of the tree; this is
None
for an empty tree.Type: ExpressionTreeNode
-
trees.tree_node
module¶
A node, and related classes, for use in expression trees.
-
class
tt.trees.tree_node.
BinaryOperatorExpressionTreeNode
(operator_str, l_child, r_child)[source]¶ Bases:
tt.trees.tree_node.ExpressionTreeNode
An expression tree node for binary operators.
-
operator
¶ The actual operator object wrapped in this node.
Type: BooleanOperator
-
-
class
tt.trees.tree_node.
ExpressionTreeNode
(symbol_name, l_child=None, r_child=None)[source]¶ Bases:
object
A base class for expression tree nodes.
-
evaluate
(input_dict)[source]¶ Recursively evaluate this node.
This is an interface that should be defined in sub-classes.
Parameters: input_dict (Dict{ str
: truthy}) – A dictionary mapping expression symbols to the value for which they should be subsituted in expression evaluation.Note
Node evaluation does no checking of the validity of inputs; they should be check before being passed here.
Returns: The evaluation of the tree rooted at this node. Return type: bool
-
l_child
¶ This node’s left child;
None
indicates the absence of a child.Type: ExpressionTreeNode
, optional
-
r_child
¶ This node’s left child;
None
indicates the absence of a child.Type: ExpressionTreeNode
, optional
-
-
class
tt.trees.tree_node.
OperandExpressionTreeNode
(operand_str)[source]¶ Bases:
tt.trees.tree_node.ExpressionTreeNode
An expression tree node for operands.
Nodes of this type will always be leaves in an expression tree.
-
class
tt.trees.tree_node.
UnaryOperatorExpressionTreeNode
(operator_str, l_child)[source]¶ Bases:
tt.trees.tree_node.ExpressionTreeNode
An expression tree node for unary operators.
-
operator
¶ The actual operator object wrapped in this node.
Type: BooleanOperator
-