CompositeVisitor¶
-
class
mlfarm.core.CompositeVisitor(*args)[source]¶ The visit method of this class will chain the object trough all the visitor parameters.
Parameters: *args – Arguments as a list, each should be an implementation of the BaseVisitor class. >>> class StrVisitor(BaseVisitor): ... def visit_str(self, obj): ... if obj == 'nok': ... return 0 ... return obj ... >>> class ListVisitor(BaseVisitor): ... def visit_list(self, obj): ... try: ... return sum(obj) ... except: ... return super().visit_list(obj) ... >>> vl = [ ... StrVisitor(), ... ListVisitor() ... ] ... >>> cv = CompositeVisitor(*vl) >>> a = {'a': [1, 2, 3], 'b': 'ok', 'c': [1, 2, 'nok']} >>> bo = vl[0].visit(a) >>> bo {'a': [1, 2, 3], 'b': 'ok', 'c': [1, 2, 0]} >>> bo = vl[1].visit(bo) >>> o = cv.visit(a) >>> o == bo True >>> o {'a': 6, 'b': 'ok', 'c': 3}