Skip to content

Commit 1176d60

Browse files
committed
Added typehint to catalog pattern
1 parent 2d89d5c commit 1176d60

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

patterns/behavioral/catalog.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,40 @@
66
__author__ = "Ibrahim Diop <[email protected]>"
77

88

9+
from typing import Callable, Dict
10+
11+
912
class Catalog:
1013
"""catalog of multiple static methods that are executed depending on an init
1114
1215
parameter
1316
"""
1417

15-
def __init__(self, param):
18+
def __init__(self, param: str) -> None:
1619

1720
# dictionary that will be used to determine which static method is
1821
# to be executed but that will be also used to store possible param
1922
# value
20-
self._static_method_choices = {'param_value_1': self._static_method_1, 'param_value_2': self._static_method_2}
23+
self._static_method_choices: Dict[Callable] = {
24+
"param_value_1": self._static_method_1,
25+
"param_value_2": self._static_method_2,
26+
}
2127

2228
# simple test to validate param value
2329
if param in self._static_method_choices.keys():
2430
self.param = param
2531
else:
26-
raise ValueError("Invalid Value for Param: {0}".format(param))
32+
raise ValueError(f"Invalid Value for Param: {param}")
2733

2834
@staticmethod
29-
def _static_method_1():
35+
def _static_method_1() -> None:
3036
print("executed method 1!")
3137

3238
@staticmethod
33-
def _static_method_2():
39+
def _static_method_2() -> None:
3440
print("executed method 2!")
3541

36-
def main_method(self):
42+
def main_method(self) -> None:
3743
"""will execute either _static_method_1 or _static_method_2
3844
3945
depending on self.param value
@@ -49,24 +55,27 @@ class CatalogInstance:
4955
parameter
5056
"""
5157

52-
def __init__(self, param):
53-
self.x1 = 'x1'
54-
self.x2 = 'x2'
58+
def __init__(self, param: str) -> None:
59+
self.x1 = "x1"
60+
self.x2 = "x2"
5561
# simple test to validate param value
5662
if param in self._instance_method_choices:
5763
self.param = param
5864
else:
59-
raise ValueError("Invalid Value for Param: {0}".format(param))
65+
raise ValueError(f"Invalid Value for Param: {param}")
6066

61-
def _instance_method_1(self):
62-
print("Value {}".format(self.x1))
67+
def _instance_method_1(self) -> None:
68+
print(f"Value {self.x1}")
6369

64-
def _instance_method_2(self):
65-
print("Value {}".format(self.x2))
70+
def _instance_method_2(self) -> None:
71+
print(f"Value {self.x2}")
6672

67-
_instance_method_choices = {'param_value_1': _instance_method_1, 'param_value_2': _instance_method_2}
73+
_instance_method_choices = {
74+
"param_value_1": _instance_method_1,
75+
"param_value_2": _instance_method_2,
76+
}
6877

69-
def main_method(self):
78+
def main_method(self) -> None:
7079
"""will execute either _instance_method_1 or _instance_method_2
7180
7281
depending on self.param value
@@ -81,25 +90,28 @@ class CatalogClass:
8190
parameter
8291
"""
8392

84-
x1 = 'x1'
85-
x2 = 'x2'
93+
x1 = "x1"
94+
x2 = "x2"
8695

87-
def __init__(self, param):
96+
def __init__(self, param: str) -> None:
8897
# simple test to validate param value
8998
if param in self._class_method_choices:
9099
self.param = param
91100
else:
92-
raise ValueError("Invalid Value for Param: {0}".format(param))
101+
raise ValueError(f"Invalid Value for Param: {param}")
93102

94103
@classmethod
95-
def _class_method_1(cls):
96-
print("Value {}".format(cls.x1))
104+
def _class_method_1(cls) -> None:
105+
print(f"Value {cls.x1}")
97106

98107
@classmethod
99-
def _class_method_2(cls):
100-
print("Value {}".format(cls.x2))
108+
def _class_method_2(cls)->None:
109+
print(f"Value {cls.x2}")
101110

102-
_class_method_choices = {'param_value_1': _class_method_1, 'param_value_2': _class_method_2}
111+
_class_method_choices: Dict[Callable] = {
112+
"param_value_1": _class_method_1,
113+
"param_value_2": _class_method_2,
114+
}
103115

104116
def main_method(self):
105117
"""will execute either _class_method_1 or _class_method_2
@@ -116,22 +128,25 @@ class CatalogStatic:
116128
parameter
117129
"""
118130

119-
def __init__(self, param):
131+
def __init__(self, param: str) -> None:
120132
# simple test to validate param value
121133
if param in self._static_method_choices:
122134
self.param = param
123135
else:
124-
raise ValueError("Invalid Value for Param: {0}".format(param))
136+
raise ValueError(f"Invalid Value for Param: {param}")
125137

126138
@staticmethod
127-
def _static_method_1():
139+
def _static_method_1() -> None:
128140
print("executed method 1!")
129141

130142
@staticmethod
131-
def _static_method_2():
143+
def _static_method_2() -> None:
132144
print("executed method 2!")
133145

134-
_static_method_choices = {'param_value_1': _static_method_1, 'param_value_2': _static_method_2}
146+
_static_method_choices: Dict[Callable] = {
147+
"param_value_1": _static_method_1,
148+
"param_value_2": _static_method_2,
149+
}
135150

136151
def main_method(self):
137152
"""will execute either _static_method_1 or _static_method_2
@@ -163,4 +178,5 @@ def main():
163178

164179
if __name__ == "__main__":
165180
import doctest
181+
166182
doctest.testmod()

0 commit comments

Comments
 (0)