|
| 1 | +""" |
| 2 | +This module implements :class:`FilterCondition`, which enables use of different filter conditions |
| 3 | +for neo.core.container.filter. |
| 4 | +
|
| 5 | +Classes: |
| 6 | + - :class:`FilterCondition`: Abstract base class for defining filter conditions. |
| 7 | + - :class:`Equals`: Filter condition to check if a value is equal to the control value. |
| 8 | + - :class:`IsNot`: Filter condition to check if a value is not equal to the control value. |
| 9 | + - :class:`LessThanOrEquals`: Filter condition to check if a value is less than or equal to the |
| 10 | + control value. |
| 11 | + - :class:`GreaterThanOrEquals`: Filter condition to check if a value is greater than or equal to |
| 12 | + the control value. |
| 13 | + - :class:`LessThan`: Filter condition to check if a value is less than the control value. |
| 14 | + - :class:`GreaterThan`: Filter condition to check if a value is greater than the control value. |
| 15 | + - :class:`IsIn`: Filter condition to check if a value is in a list or equal to the control |
| 16 | + value. |
| 17 | + - :class:`InRange`: Filter condition to check if a value is in a specified range. |
| 18 | +
|
| 19 | +The provided classes allow users to select filter conditions and use them with |
| 20 | +:func:`neo.core.container.filter()` to perform specific filtering operations on data. |
| 21 | +""" |
| 22 | +from abc import ABC, abstractmethod |
| 23 | +from numbers import Number |
| 24 | +from typing import Union, Any |
| 25 | + |
| 26 | + |
| 27 | +class FilterCondition(ABC): |
| 28 | + """ |
| 29 | + FilterCondition object is given as parameter to container.filter(): |
| 30 | +
|
| 31 | + Usage: |
| 32 | + segment.filter(my_annotation=<FilterCondition>) or |
| 33 | + segment=filter({'my_annotation': <FilterCondition>}) |
| 34 | + """ |
| 35 | + @abstractmethod |
| 36 | + def __init__(self, control: Any) -> None: |
| 37 | + """ |
| 38 | + Initialize new FilterCondition object. |
| 39 | +
|
| 40 | + Parameters: |
| 41 | + control: Any - The control value to be used for filtering. |
| 42 | +
|
| 43 | + This is an abstract base class and should not be instantiated directly. |
| 44 | + """ |
| 45 | + |
| 46 | + @abstractmethod |
| 47 | + def evaluate(self, compare: Any) -> bool: |
| 48 | + """ |
| 49 | + Evaluate the filter condition for given value. |
| 50 | +
|
| 51 | + Parameters: |
| 52 | + compare: Any - The value to be compared with the control value. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + bool: True if the condition is satisfied, False otherwise. |
| 56 | +
|
| 57 | + This method should be implemented in subclasses. |
| 58 | + """ |
| 59 | + |
| 60 | + |
| 61 | +class Equals(FilterCondition): |
| 62 | + """ |
| 63 | + Filter condition to check if target value is equal to the control value. |
| 64 | + """ |
| 65 | + def __init__(self, control: Any) -> None: |
| 66 | + self.control = control |
| 67 | + |
| 68 | + def evaluate(self, compare: Any) -> bool: |
| 69 | + return compare == self.control |
| 70 | + |
| 71 | + |
| 72 | +class IsNot(FilterCondition): |
| 73 | + """ |
| 74 | + Filter condition to check if target value is not equal to the control value. |
| 75 | + """ |
| 76 | + def __init__(self, control: Any) -> None: |
| 77 | + self.control = control |
| 78 | + |
| 79 | + def evaluate(self, compare: Any) -> bool: |
| 80 | + return compare != self.control |
| 81 | + |
| 82 | + |
| 83 | +class LessThanOrEquals(FilterCondition): |
| 84 | + """ |
| 85 | + Filter condition to check if target value is less than or equal to the control value. |
| 86 | + """ |
| 87 | + def __init__(self, control: Number) -> None: |
| 88 | + self.control = control |
| 89 | + |
| 90 | + def evaluate(self, compare: Number) -> bool: |
| 91 | + return compare <= self.control |
| 92 | + |
| 93 | + |
| 94 | +class GreaterThanOrEquals(FilterCondition): |
| 95 | + """ |
| 96 | + Filter condition to check if target value is greater than or equal to the control value. |
| 97 | + """ |
| 98 | + def __init__(self, control: Number) -> None: |
| 99 | + self.control = control |
| 100 | + |
| 101 | + def evaluate(self, compare: Number) -> bool: |
| 102 | + return compare >= self.control |
| 103 | + |
| 104 | + |
| 105 | +class LessThan(FilterCondition): |
| 106 | + """ |
| 107 | + Filter condition to check if target value is less than the control value. |
| 108 | + """ |
| 109 | + def __init__(self, control: Number) -> None: |
| 110 | + self.control = control |
| 111 | + |
| 112 | + def evaluate(self, compare: Number) -> bool: |
| 113 | + return compare < self.control |
| 114 | + |
| 115 | + |
| 116 | +class GreaterThan(FilterCondition): |
| 117 | + """ |
| 118 | + Filter condition to check if target value is greater than the control value. |
| 119 | + """ |
| 120 | + def __init__(self, control: Number) -> None: |
| 121 | + self.control = control |
| 122 | + |
| 123 | + def evaluate(self, compare: Number) -> bool: |
| 124 | + return compare > self.control |
| 125 | + |
| 126 | + |
| 127 | +class IsIn(FilterCondition): |
| 128 | + """ |
| 129 | + Filter condition to check if target is in control. |
| 130 | + """ |
| 131 | + def __init__(self, control: Union[list, tuple, set, int]) -> None: |
| 132 | + self.control = control |
| 133 | + |
| 134 | + def evaluate(self, compare: Any) -> bool: |
| 135 | + if isinstance(self.control, (list, tuple, set)): |
| 136 | + return compare in self.control |
| 137 | + if isinstance(self.control, int): |
| 138 | + return compare == self.control |
| 139 | + |
| 140 | + raise SyntaxError('parameter not of type list, tuple, set or int') |
| 141 | + |
| 142 | + |
| 143 | +class InRange(FilterCondition): |
| 144 | + """ |
| 145 | + Filter condition to check if a value is in a specified range. |
| 146 | +
|
| 147 | + Usage: |
| 148 | + InRange(upper_bound, upper_bound, left_closed=False, right_closed=False) |
| 149 | +
|
| 150 | + Parameters: |
| 151 | + lower_bound: int - The lower bound of the range. |
| 152 | + upper_bound: int - The upper bound of the range. |
| 153 | + left_closed: bool - If True, the range includes the lower bound (lower_bound <= compare). |
| 154 | + right_closed: bool - If True, the range includes the upper bound (compare <= upper_bound). |
| 155 | + """ |
| 156 | + def __init__(self, lower_bound: Number, upper_bound: Number, |
| 157 | + left_closed: bool=False, right_closed: bool=False) -> None: |
| 158 | + if not isinstance(lower_bound, Number) or not isinstance(upper_bound, Number): |
| 159 | + raise ValueError("parameter is not a number") |
| 160 | + |
| 161 | + self.lower_bound = lower_bound |
| 162 | + self.upper_bound = upper_bound |
| 163 | + self.left_closed = left_closed |
| 164 | + self.right_closed = right_closed |
| 165 | + |
| 166 | + def evaluate(self, compare: Number) -> bool: |
| 167 | + if not self.left_closed and not self.right_closed: |
| 168 | + return self.lower_bound <= compare <= self.upper_bound |
| 169 | + if not self.left_closed and self.right_closed: |
| 170 | + return self.lower_bound <= compare < self.upper_bound |
| 171 | + if self.left_closed and not self.right_closed: |
| 172 | + return self.lower_bound < compare <= self.upper_bound |
| 173 | + return self.lower_bound < compare < self.upper_bound |
0 commit comments