21
21
"""
22
22
from abc import ABC , abstractmethod
23
23
from numbers import Number
24
+ from typing import Union , Any
25
+
24
26
25
27
class FilterCondition (ABC ):
26
28
"""
@@ -31,7 +33,7 @@ class FilterCondition(ABC):
31
33
segment=filter({'my_annotation': <FilterCondition>})
32
34
"""
33
35
@abstractmethod
34
- def __init__ (self , z ):
36
+ def __init__ (self , z ) -> None :
35
37
"""
36
38
Initialize new FilterCondition object.
37
39
@@ -60,82 +62,82 @@ class Equals(FilterCondition):
60
62
"""
61
63
Filter condition to check if target value is equal to the control value.
62
64
"""
63
- def __init__ (self , z ) :
65
+ def __init__ (self , z : Any ) -> None :
64
66
self .control = z
65
67
66
- def evaluate (self , x ) :
68
+ def evaluate (self , x : Any ) -> bool :
67
69
return x == self .control
68
70
69
71
70
72
class IsNot (FilterCondition ):
71
73
"""
72
74
Filter condition to check if target value is not equal to the control value.
73
75
"""
74
- def __init__ (self , z ) :
76
+ def __init__ (self , z : Any ) -> None :
75
77
self .control = z
76
78
77
- def evaluate (self , x ) :
79
+ def evaluate (self , x : Any ) -> bool :
78
80
return x != self .control
79
81
80
82
81
83
class LessThanOrEquals (FilterCondition ):
82
84
"""
83
85
Filter condition to check if target value is less than or equal to the control value.
84
86
"""
85
- def __init__ (self , z ) :
87
+ def __init__ (self , z : Number ) -> None :
86
88
self .control = z
87
89
88
- def evaluate (self , x ) :
90
+ def evaluate (self , x : Number ) -> bool :
89
91
return x <= self .control
90
92
91
93
92
94
class GreaterThanOrEquals (FilterCondition ):
93
95
"""
94
96
Filter condition to check if target value is greater than or equal to the control value.
95
97
"""
96
- def __init__ (self , z ) :
98
+ def __init__ (self , z : Number ) -> None :
97
99
self .control = z
98
100
99
- def evaluate (self , x ) :
101
+ def evaluate (self , x : Number ) -> bool :
100
102
return x >= self .control
101
103
102
104
103
105
class LessThan (FilterCondition ):
104
106
"""
105
107
Filter condition to check if target value is less than the control value.
106
108
"""
107
- def __init__ (self , z ) :
109
+ def __init__ (self , z : Number ) -> None :
108
110
self .control = z
109
111
110
- def evaluate (self , x ) :
112
+ def evaluate (self , x : Number ) -> bool :
111
113
return x < self .control
112
114
113
115
114
116
class GreaterThan (FilterCondition ):
115
117
"""
116
118
Filter condition to check if target value is greater than the control value.
117
119
"""
118
- def __init__ (self , z ) :
120
+ def __init__ (self , z : Number ) -> None :
119
121
self .control = z
120
122
121
- def evaluate (self , x ) :
123
+ def evaluate (self , x : Number ) -> bool :
122
124
return x > self .control
123
125
124
126
125
127
class IsIn (FilterCondition ):
126
128
"""
127
129
Filter condition to check if target is in control.
128
130
"""
129
- def __init__ (self , z ) :
131
+ def __init__ (self , z : Union [ list , tuple , set , int ]) -> None :
130
132
self .control = z
131
133
132
- def evaluate (self , x ) :
134
+ def evaluate (self , x : Any ) -> bool :
133
135
if isinstance (self .control , (list , tuple , set )):
134
136
return x in self .control
135
137
if isinstance (self .control , int ):
136
138
return x == self .control
137
139
138
- raise SyntaxError ('parameter not of type list or int' )
140
+ raise SyntaxError ('parameter not of type list, tuple, set or int' )
139
141
140
142
141
143
class InRange (FilterCondition ):
@@ -151,7 +153,8 @@ class InRange(FilterCondition):
151
153
left_closed: bool - If True, the range includes the lower bound (lower_bound <= x).
152
154
right_closed: bool - If True, the range includes the upper bound (x <= upper_bound).
153
155
"""
154
- def __init__ (self , lower_bound , upper_bound , left_closed = False , right_closed = False ):
156
+ def __init__ (self , lower_bound : Number , upper_bound : Number ,
157
+ left_closed : bool = False , right_closed : bool = False ) -> None :
155
158
if not isinstance (lower_bound , Number ) or not isinstance (upper_bound , Number ):
156
159
raise ValueError ("parameter is not a number" )
157
160
@@ -160,7 +163,7 @@ def __init__(self, lower_bound, upper_bound, left_closed=False, right_closed=Fal
160
163
self .left_closed = left_closed
161
164
self .right_closed = right_closed
162
165
163
- def evaluate (self , x ) :
166
+ def evaluate (self , x : Number ) -> bool :
164
167
if not self .left_closed and not self .right_closed :
165
168
return self .lower_bound <= x <= self .upper_bound
166
169
if not self .left_closed and self .right_closed :
0 commit comments