6
6
__author__ = "Ibrahim Diop <[email protected] >"
7
7
8
8
9
+ from typing import Callable , Dict
10
+
11
+
9
12
class Catalog :
10
13
"""catalog of multiple static methods that are executed depending on an init
11
14
12
15
parameter
13
16
"""
14
17
15
- def __init__ (self , param ) :
18
+ def __init__ (self , param : str ) -> None :
16
19
17
20
# dictionary that will be used to determine which static method is
18
21
# to be executed but that will be also used to store possible param
19
22
# 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
+ }
21
27
22
28
# simple test to validate param value
23
29
if param in self ._static_method_choices .keys ():
24
30
self .param = param
25
31
else :
26
- raise ValueError ("Invalid Value for Param: {0}" . format ( param ) )
32
+ raise ValueError (f "Invalid Value for Param: { param } " )
27
33
28
34
@staticmethod
29
- def _static_method_1 ():
35
+ def _static_method_1 () -> None :
30
36
print ("executed method 1!" )
31
37
32
38
@staticmethod
33
- def _static_method_2 ():
39
+ def _static_method_2 () -> None :
34
40
print ("executed method 2!" )
35
41
36
- def main_method (self ):
42
+ def main_method (self ) -> None :
37
43
"""will execute either _static_method_1 or _static_method_2
38
44
39
45
depending on self.param value
@@ -49,24 +55,27 @@ class CatalogInstance:
49
55
parameter
50
56
"""
51
57
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"
55
61
# simple test to validate param value
56
62
if param in self ._instance_method_choices :
57
63
self .param = param
58
64
else :
59
- raise ValueError ("Invalid Value for Param: {0}" . format ( param ) )
65
+ raise ValueError (f "Invalid Value for Param: { param } " )
60
66
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 } " )
63
69
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 } " )
66
72
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
+ }
68
77
69
- def main_method (self ):
78
+ def main_method (self ) -> None :
70
79
"""will execute either _instance_method_1 or _instance_method_2
71
80
72
81
depending on self.param value
@@ -81,25 +90,28 @@ class CatalogClass:
81
90
parameter
82
91
"""
83
92
84
- x1 = 'x1'
85
- x2 = 'x2'
93
+ x1 = "x1"
94
+ x2 = "x2"
86
95
87
- def __init__ (self , param ) :
96
+ def __init__ (self , param : str ) -> None :
88
97
# simple test to validate param value
89
98
if param in self ._class_method_choices :
90
99
self .param = param
91
100
else :
92
- raise ValueError ("Invalid Value for Param: {0}" . format ( param ) )
101
+ raise ValueError (f "Invalid Value for Param: { param } " )
93
102
94
103
@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 } " )
97
106
98
107
@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 } " )
101
110
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
+ }
103
115
104
116
def main_method (self ):
105
117
"""will execute either _class_method_1 or _class_method_2
@@ -116,22 +128,25 @@ class CatalogStatic:
116
128
parameter
117
129
"""
118
130
119
- def __init__ (self , param ) :
131
+ def __init__ (self , param : str ) -> None :
120
132
# simple test to validate param value
121
133
if param in self ._static_method_choices :
122
134
self .param = param
123
135
else :
124
- raise ValueError ("Invalid Value for Param: {0}" . format ( param ) )
136
+ raise ValueError (f "Invalid Value for Param: { param } " )
125
137
126
138
@staticmethod
127
- def _static_method_1 ():
139
+ def _static_method_1 () -> None :
128
140
print ("executed method 1!" )
129
141
130
142
@staticmethod
131
- def _static_method_2 ():
143
+ def _static_method_2 () -> None :
132
144
print ("executed method 2!" )
133
145
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
+ }
135
150
136
151
def main_method (self ):
137
152
"""will execute either _static_method_1 or _static_method_2
@@ -163,4 +178,5 @@ def main():
163
178
164
179
if __name__ == "__main__" :
165
180
import doctest
181
+
166
182
doctest .testmod ()
0 commit comments