1
|
|
|
""" |
2
|
|
|
ETLT |
3
|
|
|
|
4
|
|
|
Copyright 2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
1 |
|
from etlt.condition.SimpleConditionFactory import SimpleConditionFactory |
9
|
|
|
|
10
|
1 |
|
from etlt.condition.Condition import Condition |
11
|
|
|
|
12
|
|
|
|
13
|
1 |
|
class InListCondition(Condition): |
14
|
|
|
""" |
15
|
|
|
A list condition matches a single field against a list of conditions. |
16
|
|
|
""" |
17
|
|
|
|
18
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
19
|
1 |
|
def __init__(self, field): |
20
|
|
|
""" |
21
|
|
|
Object contructor. |
22
|
|
|
|
23
|
|
|
:param str field: The name of the field in the row that must be match against the expression. |
24
|
|
|
""" |
25
|
1 |
|
self._field = field |
26
|
|
|
""" |
27
|
|
|
The name of the field in the row that must be match against the list of values. |
28
|
|
|
|
29
|
|
|
:type: str |
30
|
|
|
""" |
31
|
|
|
|
32
|
1 |
|
self._values = list() |
33
|
|
|
""" |
34
|
|
|
The list of values of plain conditions. |
35
|
|
|
|
36
|
|
|
:type: list[str] |
37
|
|
|
""" |
38
|
|
|
|
39
|
1 |
|
self._conditions = list() |
40
|
1 |
|
""" |
41
|
|
|
The list of other conditions. |
42
|
|
|
|
43
|
|
|
:type: list[etlt.condition.SimpleCondition.SimpleCondition] |
44
|
|
|
""" |
45
|
|
|
|
46
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
47
|
1 |
|
@property |
48
|
|
|
def field(self): |
49
|
|
|
""" |
50
|
|
|
Getter for field. |
51
|
|
|
|
52
|
|
|
:rtype: str |
53
|
|
|
""" |
54
|
|
|
return self._field |
55
|
|
|
|
56
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
57
|
1 |
|
def populate_values(self, rows, field): |
58
|
|
|
""" |
59
|
|
|
Populates the filter values of this filter using list of rows. |
60
|
|
|
|
61
|
|
|
:param list[dict[str,T]] rows: The row set. |
62
|
|
|
:param str field: The field name. |
63
|
|
|
""" |
64
|
1 |
|
self._values.clear() |
65
|
1 |
|
for row in rows: |
66
|
1 |
|
condition = SimpleConditionFactory.create_condition(self._field, row[field]) |
67
|
1 |
|
if condition.scheme == 'plain': |
68
|
1 |
|
self._values.append(condition.expression) |
69
|
|
|
else: |
70
|
1 |
|
self._conditions.append(condition) |
71
|
|
|
|
72
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
73
|
1 |
|
def match(self, row): |
74
|
|
|
""" |
75
|
|
|
Returns True if the field is in the list of conditions. Returns False otherwise. |
76
|
|
|
|
77
|
|
|
:param dict row: The row. |
78
|
|
|
|
79
|
|
|
:rtype: bool |
80
|
|
|
""" |
81
|
1 |
|
if row[self._field] in self._values: |
82
|
1 |
|
return True |
83
|
|
|
|
84
|
1 |
|
for condition in self._conditions: |
85
|
1 |
|
if condition.match(row): |
86
|
1 |
|
return True |
87
|
|
|
|
88
|
1 |
|
return False |
89
|
|
|
|
90
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
91
|
|
|
|