trees

Tools for working with Boolean expression trees.

tt.expr_tree module

An expression tree implementation for Boolean expressions.

class tt.trees.expr_tree.BooleanExpressionTree(postfix_tokens)

Bases: object

An expression tree for Boolean expressions.

Note

This class expects any input it receives to be well-formed; any tokenized lists you pass it directly (instead of through the BooleanExpression class) will not be checked.

postfix_tokens

List[str]

The tokens in the expression from which to build the tree.

root

tt.trees.ExpressionTreeNode

The root of the tree; None for an empty tree.

evaluate(input_dict)

Evaluate the expression held in this tree for specified inputs.

Parameters:input_dict (Dict) – A dict mapping string variable names to the values for which they should be evaluated.
Returns:The truthy result of the expression tree evaluation.

Note

This function does not check to ensure the validity of
input_dict in any way.

tt.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)

Bases: tt.trees.tree_node.ExpressionTreeNode

An expression tree node for binary operators.

operator

tt.operators.BooleanOperator

The actual operator wrapped in this node.

class tt.trees.tree_node.ExpressionTreeNode(symbol_name, l_child=None, r_child=None)

Bases: object

A base class for expression tree nodes.

symbol_name

str

The string operator/operand name of wrapped in this node.

l_child

tt.trees.ExpressionTreeNode, optional

This node’s left child.

r_child

tt.trees.ExpressionTreeNode, optional

This node’s right child.

evaluate(input_dict)

Recursively evaluate this node.

Parameters:input_dict (Dict) – 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:A truthy value.
class tt.trees.tree_node.OperandExpressionTreeNode(operand_str)

Bases: tt.trees.tree_node.ExpressionTreeNode

An expression tree node for operands.

class tt.trees.tree_node.UnaryOperatorExpressionTreeNode(operator_str, l_child)

Bases: tt.trees.tree_node.ExpressionTreeNode

An expression tree node for unary operators.