Skip to content

Commit 9ee9b44

Browse files
authored
Merge pull request #339 from rednafi/master
Added type hints to catalog pattern
2 parents fffc37f + af85c4b commit 9ee9b44

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

patterns/behavioral/catalog.py

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,31 @@ class Catalog:
1212
parameter
1313
"""
1414

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

1717
# dictionary that will be used to determine which static method is
1818
# to be executed but that will be also used to store possible param
1919
# value
20-
self._static_method_choices = {'param_value_1': self._static_method_1, 'param_value_2': self._static_method_2}
20+
self._static_method_choices = {
21+
"param_value_1": self._static_method_1,
22+
"param_value_2": self._static_method_2,
23+
}
2124

2225
# simple test to validate param value
2326
if param in self._static_method_choices.keys():
2427
self.param = param
2528
else:
26-
raise ValueError("Invalid Value for Param: {0}".format(param))
29+
raise ValueError(f"Invalid Value for Param: {param}")
2730

2831
@staticmethod
29-
def _static_method_1():
32+
def _static_method_1() -> None:
3033
print("executed method 1!")
3134

3235
@staticmethod
33-
def _static_method_2():
36+
def _static_method_2() -> None:
3437
print("executed method 2!")
3538

36-
def main_method(self):
39+
def main_method(self) -> None:
3740
"""will execute either _static_method_1 or _static_method_2
3841
3942
depending on self.param value
@@ -49,24 +52,27 @@ class CatalogInstance:
4952
parameter
5053
"""
5154

52-
def __init__(self, param):
53-
self.x1 = 'x1'
54-
self.x2 = 'x2'
55+
def __init__(self, param: str) -> None:
56+
self.x1 = "x1"
57+
self.x2 = "x2"
5558
# simple test to validate param value
5659
if param in self._instance_method_choices:
5760
self.param = param
5861
else:
59-
raise ValueError("Invalid Value for Param: {0}".format(param))
62+
raise ValueError(f"Invalid Value for Param: {param}")
6063

61-
def _instance_method_1(self):
62-
print("Value {}".format(self.x1))
64+
def _instance_method_1(self) -> None:
65+
print(f"Value {self.x1}")
6366

64-
def _instance_method_2(self):
65-
print("Value {}".format(self.x2))
67+
def _instance_method_2(self) -> None:
68+
print(f"Value {self.x2}")
6669

67-
_instance_method_choices = {'param_value_1': _instance_method_1, 'param_value_2': _instance_method_2}
70+
_instance_method_choices = {
71+
"param_value_1": _instance_method_1,
72+
"param_value_2": _instance_method_2,
73+
}
6874

69-
def main_method(self):
75+
def main_method(self) -> None:
7076
"""will execute either _instance_method_1 or _instance_method_2
7177
7278
depending on self.param value
@@ -81,25 +87,28 @@ class CatalogClass:
8187
parameter
8288
"""
8389

84-
x1 = 'x1'
85-
x2 = 'x2'
90+
x1 = "x1"
91+
x2 = "x2"
8692

87-
def __init__(self, param):
93+
def __init__(self, param: str) -> None:
8894
# simple test to validate param value
8995
if param in self._class_method_choices:
9096
self.param = param
9197
else:
92-
raise ValueError("Invalid Value for Param: {0}".format(param))
98+
raise ValueError(f"Invalid Value for Param: {param}")
9399

94100
@classmethod
95-
def _class_method_1(cls):
96-
print("Value {}".format(cls.x1))
101+
def _class_method_1(cls) -> None:
102+
print(f"Value {cls.x1}")
97103

98104
@classmethod
99-
def _class_method_2(cls):
100-
print("Value {}".format(cls.x2))
105+
def _class_method_2(cls) -> None:
106+
print(f"Value {cls.x2}")
101107

102-
_class_method_choices = {'param_value_1': _class_method_1, 'param_value_2': _class_method_2}
108+
_class_method_choices = {
109+
"param_value_1": _class_method_1,
110+
"param_value_2": _class_method_2,
111+
}
103112

104113
def main_method(self):
105114
"""will execute either _class_method_1 or _class_method_2
@@ -116,28 +125,32 @@ class CatalogStatic:
116125
parameter
117126
"""
118127

119-
def __init__(self, param):
128+
def __init__(self, param: str) -> None:
120129
# simple test to validate param value
121130
if param in self._static_method_choices:
122131
self.param = param
123132
else:
124-
raise ValueError("Invalid Value for Param: {0}".format(param))
133+
raise ValueError(f"Invalid Value for Param: {param}")
125134

126135
@staticmethod
127-
def _static_method_1():
136+
def _static_method_1() -> None:
128137
print("executed method 1!")
129138

130139
@staticmethod
131-
def _static_method_2():
140+
def _static_method_2() -> None:
132141
print("executed method 2!")
133142

134-
_static_method_choices = {'param_value_1': _static_method_1, 'param_value_2': _static_method_2}
143+
_static_method_choices = {
144+
"param_value_1": _static_method_1,
145+
"param_value_2": _static_method_2,
146+
}
135147

136-
def main_method(self):
148+
def main_method(self) -> None:
137149
"""will execute either _static_method_1 or _static_method_2
138150
139151
depending on self.param value
140152
"""
153+
141154
self._static_method_choices[self.param].__get__(None, self.__class__)()
142155

143156

@@ -163,4 +176,5 @@ def main():
163176

164177
if __name__ == "__main__":
165178
import doctest
179+
166180
doctest.testmod()

0 commit comments

Comments
 (0)