class documentation

class Visitor(Generic[T], abc.ABC): (source)

View In Hierarchy

"Visitor" pattern abstract superclass implementation for tree traversals.

Each class has corresponding methods, doing nothing by default; override individual methods for specific and useful behaviour. The visit() method is called by walk() upon entering a object. walkabout() also calls the depart() method before exiting a object.

The generic methods call "visit_ + objet class name" or "depart_ + objet class name", resp.

This is a base class for visitors whose visit_... & depart_... methods should be implemented for all concrete objets types encountered.

Class SkipChildren Do not visit any children of the current node. The current node's siblings and depart_... method are not affected.
Class SkipDeparture Do not call the current node's depart_... method. The current node's children and siblings are not affected.
Class SkipNode Do not visit the current node's children, and do not call the current node's depart_... method.
Class SkipSiblings Do not visit any more siblings (to the right) of the current node. The current node's children and its depart_... method are not affected.
Method depart Depart an object.
Method get_children Undocumented
Method unknown_departure Called before exiting unknown object types.
Method unknown_visit Called when entering unknown object types.
Method visit Visit an object.
Method walk Traverse a tree of objects, calling the visit() method of visitor when entering each node. (The walkabout() method is similar, except it also calls the depart() method before exiting each objects.)
Method walkabout Perform a tree traversal similarly to walk() (which see), except also call the depart() method before exiting each node.
Class _TreePruningException Base class for Visitor-related tree pruning exceptions.
def depart(self, ob): (source)

Depart an object.

Parameters
ob:TUndocumented
@abc.abstractclassmethod
def get_children(cls, ob): (source)

Undocumented

Parameters
ob:TUndocumented
Returns
Iterable[T]Undocumented
def unknown_departure(self, ob): (source)

Called before exiting unknown object types.

Raise exception unless overridden.

Parameters
ob:TUndocumented
def unknown_visit(self, ob): (source)

Called when entering unknown object types.

Raise an exception unless overridden.

Parameters
ob:TUndocumented
def visit(self, ob): (source)

Visit an object.

Parameters
ob:TUndocumented
def walk(self, ob): (source)

Traverse a tree of objects, calling the visit() method of visitor when entering each node. (The walkabout() method is similar, except it also calls the depart() method before exiting each objects.)

This tree traversal supports limited in-place tree modifications. Replacing one node with one or more nodes is OK, as is removing an element. However, if the node removed or replaced occurs after the current node, the old node will still be traversed, and any new nodes will not.

Parameters
ob:TAn object to walk.
visitorA Visitor object, containing a visit implementation for each object type encountered.
get_childrenA callable that returns the children of an object.
def walkabout(self, ob): (source)

Perform a tree traversal similarly to walk() (which see), except also call the depart() method before exiting each node.

Parameters
ob:TAn object to walk.
visitorA Visitor object, containing a visit and depart implementation for each concrete object type encountered.
get_childrenA callable that returns the children of an object.