@@ -46,6 +46,108 @@ def main_method(self):
46
46
self ._static_method_choices [self .param ]()
47
47
48
48
49
+ # Alternative implementation for different levels of methods
50
+ class CatalogInstance :
51
+
52
+ """
53
+ catalog of multiple methods that are executed depending on an init
54
+ parameter
55
+ """
56
+
57
+ def __init__ (self , param ):
58
+ self .x1 = 'x1'
59
+ self .x2 = 'x2'
60
+ # simple test to validate param value
61
+ if param in self ._instance_method_choices :
62
+ self .param = param
63
+ else :
64
+ raise ValueError ("Invalid Value for Param: {0}" .format (param ))
65
+
66
+ def _instance_method_1 (self ):
67
+ print ("Value {}" .format (self .x1 ))
68
+
69
+ def _instance_method_2 (self ):
70
+ print ("Value {}" .format (self .x2 ))
71
+
72
+ _instance_method_choices = {'param_value_1' : _instance_method_1 ,
73
+ 'param_value_2' : _instance_method_2 }
74
+
75
+
76
+ def main_method (self ):
77
+ """
78
+ will execute either _instance_method_1 or _instance_method_1
79
+ depending on self.param value
80
+ """
81
+ self ._instance_method_choices [self .param ].__get__ (self )()
82
+
83
+
84
+ class CatalogClass :
85
+
86
+ """
87
+ catalog of multiple class methods that are executed depending on an init
88
+ parameter
89
+ """
90
+
91
+ x1 = 'x1'
92
+ x2 = 'x2'
93
+
94
+ def __init__ (self , param ):
95
+ # simple test to validate param value
96
+ if param in self ._class_method_choices :
97
+ self .param = param
98
+ else :
99
+ raise ValueError ("Invalid Value for Param: {0}" .format (param ))
100
+
101
+ @classmethod
102
+ def _class_method_1 (cls ):
103
+ print ("Value {}" .format (cls .x1 ))
104
+
105
+ @classmethod
106
+ def _class_method_2 (cls ):
107
+ print ("Value {}" .format (cls .x2 ))
108
+
109
+ _class_method_choices = {'param_value_1' : _class_method_1 ,
110
+ 'param_value_2' : _class_method_2 }
111
+
112
+ def main_method (self ):
113
+ """
114
+ will execute either _instance_method_1 or _instance_method_1
115
+ depending on self.param value
116
+ """
117
+ self ._class_method_choices [self .param ].__get__ (None , self .__class__ )()
118
+
119
+
120
+ class CatalogStatic :
121
+
122
+ """
123
+ catalog of multiple static methods that are executed depending on an init
124
+ parameter
125
+ """
126
+ def __init__ (self , param ):
127
+ # simple test to validate param value
128
+ if param in self ._static_method_choices :
129
+ self .param = param
130
+ else :
131
+ raise ValueError ("Invalid Value for Param: {0}" .format (param ))
132
+
133
+ @staticmethod
134
+ def _static_method_1 ():
135
+ print ("executed method 1!" )
136
+
137
+ @staticmethod
138
+ def _static_method_2 ():
139
+ print ("executed method 2!" )
140
+
141
+ _static_method_choices = {'param_value_1' : _static_method_1 ,
142
+ 'param_value_2' : _static_method_2 }
143
+
144
+ def main_method (self ):
145
+ """
146
+ will execute either _instance_method_1 or _instance_method_1
147
+ depending on self.param value
148
+ """
149
+ self ._static_method_choices [self .param ].__get__ (None , self .__class__ )()
150
+
49
151
def main ():
50
152
"""
51
153
>>> c = Catalog('param_value_1').main_method()
@@ -57,8 +159,20 @@ def main():
57
159
test = Catalog ('param_value_2' )
58
160
test .main_method ()
59
161
162
+ test = CatalogInstance ('param_value_1' )
163
+ test .main_method ()
164
+
165
+ test = CatalogClass ('param_value_2' )
166
+ test .main_method ()
167
+
168
+ test = CatalogStatic ('param_value_1' )
169
+ test .main_method ()
170
+
60
171
if __name__ == "__main__" :
61
172
main ()
62
173
63
174
### OUTPUT ###
64
175
# executed method 2!
176
+ # Value x1
177
+ # Value x2
178
+ # executed method 1!
0 commit comments