Skip to content

tbhartman/doorbell

Repository files navigation

doorbell

Build Status Coverage Status Docs Status

PyPI version Python Version

doorbell provides a visitor pattern implementation for Python.

Usage

Define an object that receives a visitor. For example, in an expression evaluator, create classes for a value and add and multiply operators:

@doorbell.Visitee.create
class Value:
    def __init__(self, value=0):
        self.value = value
        self.children = []


@doorbell.Visitee.create
class Add(Value):
    pass


@doorbell.Visitee.create('Multiply')
class Mult(Add):
    pass

Then create a visitor class to evaluate:

class Visitor(doorbell.CascadingVisitor):
    def visit_Value(self, obj, children):
        return obj.value

    def visit_Add(self, obj, children):
        return functools.reduce(operator.add, children, 0)

    def visit_Multiply(self, obj, children):
        return functools.reduce(operator.mul, children, 1)

Create an object and visit it:

// (1+1)*(1+1)
one = Value(1)
add = Add()
add.children.extend((one, one))
mult = Mult()
mult.children.extend((add, add))

v = Visitor()
r = mult.accept(v)
assert r == 4

Installation

pip install doorbell

License

doorbell is free software and licensed under the MIT License.