Tree matcher

Module to do pattern matching with trees.

It does with patterns and trees something similar to what the re module does with expressions and strings.

class TreePattern(pattern='', children=None, parser=1, safer=False)[source]

A pattern of conditions to be satisfied by a subtree in a tree.

It stores in the node names the constraints for that node.

__init__(pattern='', children=None, parser=1, safer=False)[source]
Parameters:
  • pattern – Tree pattern to match, as a newick string. Optionally with conditions on the nodes too (in place of node names).

  • safer – If True, calls to eval() will be safer by strongly restricting the Python keywords that can be used.

The other parameters (children and parser) are needed to call Tree’s constructor appropriately, but should not be used when creating a TreePattern.

match(tree, context=None)[source]
search(tree, context=None, strategy='levelorder')[source]
match(pattern, node, context=None)[source]

Return True if the pattern matches the given node.

safer_eval(code, context)[source]

Return a safer version of eval(code, context).

search(pattern, tree, context=None, strategy='levelorder')[source]

Yield nodes that match the given pattern.