- Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy pathcatalog.py
180 lines (132 loc) · 4.76 KB
/
catalog.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
A class that uses different static function depending of a parameter passed in
init. Note the use of a single dictionary instead of multiple conditions
"""
__author__="Ibrahim Diop <ibrahim@sikilabs.com>"
classCatalog:
"""catalog of multiple static methods that are executed depending on an init
parameter
"""
def__init__(self, param: str) ->None:
# dictionary that will be used to determine which static method is
# to be executed but that will be also used to store possible param
# value
self._static_method_choices= {
"param_value_1": self._static_method_1,
"param_value_2": self._static_method_2,
}
# simple test to validate param value
ifparaminself._static_method_choices.keys():
self.param=param
else:
raiseValueError(f"Invalid Value for Param: {param}")
@staticmethod
def_static_method_1() ->None:
print("executed method 1!")
@staticmethod
def_static_method_2() ->None:
print("executed method 2!")
defmain_method(self) ->None:
"""will execute either _static_method_1 or _static_method_2
depending on self.param value
"""
self._static_method_choices[self.param]()
# Alternative implementation for different levels of methods
classCatalogInstance:
"""catalog of multiple methods that are executed depending on an init
parameter
"""
def__init__(self, param: str) ->None:
self.x1="x1"
self.x2="x2"
# simple test to validate param value
ifparaminself._instance_method_choices:
self.param=param
else:
raiseValueError(f"Invalid Value for Param: {param}")
def_instance_method_1(self) ->None:
print(f"Value {self.x1}")
def_instance_method_2(self) ->None:
print(f"Value {self.x2}")
_instance_method_choices= {
"param_value_1": _instance_method_1,
"param_value_2": _instance_method_2,
}
defmain_method(self) ->None:
"""will execute either _instance_method_1 or _instance_method_2
depending on self.param value
"""
self._instance_method_choices[self.param].__get__(self)() # type: ignore
# type ignore reason: https://github.com/python/mypy/issues/10206
classCatalogClass:
"""catalog of multiple class methods that are executed depending on an init
parameter
"""
x1="x1"
x2="x2"
def__init__(self, param: str) ->None:
# simple test to validate param value
ifparaminself._class_method_choices:
self.param=param
else:
raiseValueError(f"Invalid Value for Param: {param}")
@classmethod
def_class_method_1(cls) ->None:
print(f"Value {cls.x1}")
@classmethod
def_class_method_2(cls) ->None:
print(f"Value {cls.x2}")
_class_method_choices= {
"param_value_1": _class_method_1,
"param_value_2": _class_method_2,
}
defmain_method(self):
"""will execute either _class_method_1 or _class_method_2
depending on self.param value
"""
self._class_method_choices[self.param].__get__(None, self.__class__)() # type: ignore
# type ignore reason: https://github.com/python/mypy/issues/10206
classCatalogStatic:
"""catalog of multiple static methods that are executed depending on an init
parameter
"""
def__init__(self, param: str) ->None:
# simple test to validate param value
ifparaminself._static_method_choices:
self.param=param
else:
raiseValueError(f"Invalid Value for Param: {param}")
@staticmethod
def_static_method_1() ->None:
print("executed method 1!")
@staticmethod
def_static_method_2() ->None:
print("executed method 2!")
_static_method_choices= {
"param_value_1": _static_method_1,
"param_value_2": _static_method_2,
}
defmain_method(self) ->None:
"""will execute either _static_method_1 or _static_method_2
depending on self.param value
"""
self._static_method_choices[self.param].__get__(None, self.__class__)() # type: ignore
# type ignore reason: https://github.com/python/mypy/issues/10206
defmain():
"""
>>> test = Catalog('param_value_2')
>>> test.main_method()
executed method 2!
>>> test = CatalogInstance('param_value_1')
>>> test.main_method()
Value x1
>>> test = CatalogClass('param_value_2')
>>> test.main_method()
Value x2
>>> test = CatalogStatic('param_value_1')
>>> test.main_method()
executed method 1!
"""
if__name__=="__main__":
importdoctest
doctest.testmod()