comedy.visitor — Simple visitor pattern

This module provides a simple straightforward implementation of visitor pattern.

>>> compile = Visitor("compile")
>>> @compile.visit(int)
... @compile.visit(long)
... def compile(value):
...     return str(value)
...
>>> @compile.visit(basestring)
... def compile(value):
...     return "'{0}'".format(value)
...
>>> @compile.visit(unicode)
... def compile(value):
...     return "u" + compile[basestring](value)
...
>>> @compile.visit(list)
... def compile(value):
...     return "[{0}]".format(", ".join(compile(v) for v in value))
...
>>> compile([1, 2, "3", u"4", [5, 6]])
"[1, 2, '3', u'4', [5, 6]]"
class comedy.visitor.Visitor(name)

Visitor function object.

>>> compile = Visitor("compile")
>>> compile  
<comedy.visitor.Visitor 'compile' at ...>
>>> compile.__name__
'compile'
Parameters:name – the name of the visitor function
functions

Registered functions by types in dict.

latest_added_function

The latest added function. May be None.

__name__

The name of the visitor function.

visit(type)

Returns a decorator to register a function.

>>> compile = Visitor("compile")
>>> @compile.visit(int)
... @compile.visit(long)
... def compile(value):
...     return "int: {0}".format(value)
...
Parameters:type – a type to connect with a function
Returns:an VisitorDecorator instance
class comedy.visitor.VisitorDecorator(visitor, type)

Decorator function object to register a function to Visitor. Internally used.

Parameters:
  • visitor – an Visitor instance
  • type (types.TypeType) – a type object
visitor

The Visitor instance.

type

The type object, types.TypeType instance.

Project Versions

Previous topic

comedy.parser — Veeyu parser

Next topic

comedy.compiler — Veeyu to Python compiler

This Page