Skip to content

Commit 4cffba0

Browse files
committed
Add alternative implementation of catalog for different levels of methods
1 parent 2c29478 commit 4cffba0

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

catalog.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,108 @@ def main_method(self):
4646
self._static_method_choices[self.param]()
4747

4848

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+
49151
def main():
50152
"""
51153
>>> c = Catalog('param_value_1').main_method()
@@ -57,8 +159,20 @@ def main():
57159
test = Catalog('param_value_2')
58160
test.main_method()
59161

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+
60171
if __name__ == "__main__":
61172
main()
62173

63174
### OUTPUT ###
64175
# executed method 2!
176+
# Value x1
177+
# Value x2
178+
# executed method 1!

0 commit comments

Comments
 (0)