11"""
2- A class that uses different static function depending of a parameter passed in
3- init. Note the use of a single dictionary instead of multiple conditions
2+ A class that uses a different static function depending on a parameter passed in
3+ init. Note the use of a single dictionary instead of multiple conditions.
44"""
55
66__author__ = "Ibrahim Diop <[email protected] >" 77
88
99class Catalog :
10- """catalog of multiple static methods that are executed depending on an init
11-
12- parameter
10+ """catalog of multiple static methods that are executed depending on an init parameter
1311 """
1412
1513 def __init__ (self , param : str ) -> None :
@@ -29,27 +27,24 @@ def __init__(self, param: str) -> None:
2927 raise ValueError (f"Invalid Value for Param: { param } " )
3028
3129 @staticmethod
32- def _static_method_1 () -> None :
33- print ( "executed method 1!" )
30+ def _static_method_1 () -> str :
31+ return "executed method 1!"
3432
3533 @staticmethod
36- def _static_method_2 () -> None :
37- print ( "executed method 2!" )
34+ def _static_method_2 () -> str :
35+ return "executed method 2!"
3836
39- def main_method (self ) -> None :
37+ def main_method (self ) -> str :
4038 """will execute either _static_method_1 or _static_method_2
4139
4240 depending on self.param value
4341 """
44- self ._static_method_choices [self .param ]()
42+ return self ._static_method_choices [self .param ]()
4543
4644
4745# Alternative implementation for different levels of methods
4846class CatalogInstance :
49-
50- """catalog of multiple methods that are executed depending on an init
51-
52- parameter
47+ """catalog of multiple methods that are executed depending on an init parameter
5348 """
5449
5550 def __init__ (self , param : str ) -> None :
@@ -61,31 +56,28 @@ def __init__(self, param: str) -> None:
6156 else :
6257 raise ValueError (f"Invalid Value for Param: { param } " )
6358
64- def _instance_method_1 (self ) -> None :
65- print ( f"Value { self .x1 } " )
59+ def _instance_method_1 (self ) -> str :
60+ return f"Value { self .x1 } "
6661
67- def _instance_method_2 (self ) -> None :
68- print ( f"Value { self .x2 } " )
62+ def _instance_method_2 (self ) -> str :
63+ return f"Value { self .x2 } "
6964
7065 _instance_method_choices = {
7166 "param_value_1" : _instance_method_1 ,
7267 "param_value_2" : _instance_method_2 ,
7368 }
7469
75- def main_method (self ) -> None :
70+ def main_method (self ) -> str :
7671 """will execute either _instance_method_1 or _instance_method_2
7772
7873 depending on self.param value
7974 """
80- self ._instance_method_choices [self .param ].__get__ (self )() # type: ignore
75+ return self ._instance_method_choices [self .param ].__get__ (self )() # type: ignore
8176 # type ignore reason: https://github.com/python/mypy/issues/10206
8277
8378
8479class CatalogClass :
85-
86- """catalog of multiple class methods that are executed depending on an init
87-
88- parameter
80+ """catalog of multiple class methods that are executed depending on an init parameter
8981 """
9082
9183 x1 = "x1"
@@ -99,32 +91,29 @@ def __init__(self, param: str) -> None:
9991 raise ValueError (f"Invalid Value for Param: { param } " )
10092
10193 @classmethod
102- def _class_method_1 (cls ) -> None :
103- print ( f"Value { cls .x1 } " )
94+ def _class_method_1 (cls ) -> str :
95+ return f"Value { cls .x1 } "
10496
10597 @classmethod
106- def _class_method_2 (cls ) -> None :
107- print ( f"Value { cls .x2 } " )
98+ def _class_method_2 (cls ) -> str :
99+ return f"Value { cls .x2 } "
108100
109101 _class_method_choices = {
110102 "param_value_1" : _class_method_1 ,
111103 "param_value_2" : _class_method_2 ,
112104 }
113105
114- def main_method (self ):
106+ def main_method (self ) -> str :
115107 """will execute either _class_method_1 or _class_method_2
116108
117109 depending on self.param value
118110 """
119- self ._class_method_choices [self .param ].__get__ (None , self .__class__ )() # type: ignore
111+ return self ._class_method_choices [self .param ].__get__ (None , self .__class__ )() # type: ignore
120112 # type ignore reason: https://github.com/python/mypy/issues/10206
121113
122114
123115class CatalogStatic :
124-
125- """catalog of multiple static methods that are executed depending on an init
126-
127- parameter
116+ """catalog of multiple static methods that are executed depending on an init parameter
128117 """
129118
130119 def __init__ (self , param : str ) -> None :
@@ -135,45 +124,45 @@ def __init__(self, param: str) -> None:
135124 raise ValueError (f"Invalid Value for Param: { param } " )
136125
137126 @staticmethod
138- def _static_method_1 () -> None :
139- print ( "executed method 1!" )
127+ def _static_method_1 () -> str :
128+ return "executed method 1!"
140129
141130 @staticmethod
142- def _static_method_2 () -> None :
143- print ( "executed method 2!" )
131+ def _static_method_2 () -> str :
132+ return "executed method 2!"
144133
145134 _static_method_choices = {
146135 "param_value_1" : _static_method_1 ,
147136 "param_value_2" : _static_method_2 ,
148137 }
149138
150- def main_method (self ) -> None :
139+ def main_method (self ) -> str :
151140 """will execute either _static_method_1 or _static_method_2
152141
153142 depending on self.param value
154143 """
155144
156- self ._static_method_choices [self .param ].__get__ (None , self .__class__ )() # type: ignore
145+ return self ._static_method_choices [self .param ].__get__ (None , self .__class__ )() # type: ignore
157146 # type ignore reason: https://github.com/python/mypy/issues/10206
158147
159148
160149def main ():
161150 """
162151 >>> test = Catalog('param_value_2')
163152 >>> test.main_method()
164- executed method 2!
153+ ' executed method 2!'
165154
166155 >>> test = CatalogInstance('param_value_1')
167156 >>> test.main_method()
168- Value x1
157+ ' Value x1'
169158
170159 >>> test = CatalogClass('param_value_2')
171160 >>> test.main_method()
172- Value x2
161+ ' Value x2'
173162
174163 >>> test = CatalogStatic('param_value_1')
175164 >>> test.main_method()
176- executed method 1!
165+ ' executed method 1!'
177166 """
178167
179168
0 commit comments