Usage

The Visitee

doorbell provides Visitee, an abstract base class with a single method, Visitee.accept(). Implementations of Visitee.accept() typically only consist of a single line:

def accept(self, visitor):
   return visitor.visit_MyType(self)

where visit_MyType is the method on the visitor which applies to this particular object. Typically, only the object (self) is passed, although any arguments will be passed along to the visitor’s method.

Several decoratormethod are provided to aid in creating the accept method:

create Decorator to create accept method on class
auto_create Decorator to automatically create accept methods on subclasses
stop_auto_create Stop auto-creating accept methods

The Visitor

The base Visitor class and its children are the main products of doorbell. Your visitor class inherits from Visitor or its children, and implements a set of methods which are called from a Visitee.accpet(). By default, any method whose name begins visit_ is considered a visitor method. However, the decorators:

visitor_method Wrapper to mark non-default method as a visitor method.
non_visitor_method Wrapper to mark method as not a visitor method.

override this default. Any method decorated with visitor_method() will be considered a visitor method, while any method decorated with non_visitor_method() will not be considered a visitor method. All visitor methods are wrapped by Visitor._visit_method().

The following visitor classes are provided:

Visitor A basic visitor.
CascadingVisitor Visits children first.
WrappingVisitor A visitor class that wraps each call with pre and post methods.