visitor_design_pattern package¶
Submodules¶
visitor_design_pattern.visitor_design_pattern module¶
Visitor Design Pattern main module
- This module defines the main component exposed by the visitor-design-pattern package:
visitor class decorator
traverse method decorator
prefix method decorator
infix method decorator
suffix method decorator
Visitable interface
- class visitor_design_pattern.visitor_design_pattern.VisitableInterface[source]¶
Bases:
objectBase class for visited nodes of the data structure
- accept(visitor, parent_res=None)[source]¶
Default accept method
This method implement the base logic of the traversal.
A complex data structure may need a custom implementation. In that case users may subclass the VisitableInterface and implement their own traversal logic.
- Parameters:
visitor: class decorated by the visitor class decorator
parent_res: return value of the prefix call of visitor on the current node’s parent
- visitor_design_pattern.visitor_design_pattern.infix()[source]¶
infix visit method decorator
Equivalent to traverse(‘infix’)
- visitor_design_pattern.visitor_design_pattern.prefix()[source]¶
prefix visit method decorator
Equivalent to traverse(‘prefix’)
- visitor_design_pattern.visitor_design_pattern.suffix()[source]¶
suffix visit method decorator
Equivalent to traverse(‘suffix’)
- visitor_design_pattern.visitor_design_pattern.traverse(traversal_mode)[source]¶
visit method decorator
Mark a method as visit method
Usage:
@visitor() def MyVisitor(): @traverse("prefix") def my_visit_method(self, node: Type): ... ...
- Parameters:
- traversal_mode: “prefix”, “infix” or “suffix” or list
The specified mode defines when the visit method should be called when traversing the structure.
A list of multiple modes can be specified.
- visitor_design_pattern.visitor_design_pattern.visitor(traversal_mode=None)[source]¶
visitor class decorator
Mark a class cls as visitor. This initializes the supported VISITABLE_TYPES dict in cls, and gather the different visit method marked with the traverse, prefix, infix or suffix decorators
Usage:
@visitor() def MyVisitor(): ...
- Parameters:
- traversal_mode: None, “prefix”, “infix” or “suffix”
Specifying a non None traversal_mode prevents other modes to be used in the decorated visitor
Module contents¶
Top-level package for Visitor Design Pattern.