-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_visitor.py
61 lines (44 loc) · 1.38 KB
/
py_visitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class File(object):
def accept(self, v):
v.visit(self)
class Text(File):
data = "Text"
class Picture(File):
data = "Picture"
class Visitor(object):
def visit(self, o):
method_name = "visit_" + o.__class__.__name__
method = getattr(self, method_name, False)
# if there is no attribute 'visit_ClassName' in Visitor class
# error message will be printed
if not method:
self.error_message(o)
return
if callable(method):
method(o)
else:
print "%s is not callable attribute"%(method_name)
def error_message(self, o):
print " Error: '%s' can't save '%s'"%(self.__class__.__name__, o.data)
class txtVisitor(Visitor):
def visit_Text(self, o):
print " Saving '%s' in 'txt' format"%(o.data)
class jpgVisitor(Visitor):
def visit_Picture(self, o):
print " Saving '%s' in 'jpg' format"%(o.data)
class blobVisitor(Visitor):
def visit_Text(self, o):
print " Saving '%s' in 'blob' format"%(o.data)
def visit_Picture(self, o):
print " Saving '%s' in 'blob' format"%(o.data)
def main():
lst = [Text(), Picture()]
vst = [txtVisitor(), jpgVisitor(), blobVisitor()]
for o in lst:
print "Object is: %s"%o.data
for v in vst:
#First dispatch, accept() will be called for needed type of object:
o.accept(v)
print ""
if __name__ == "__main__":
main()