Completed
Push — master ( 9d90ba...7d87fd )
by P.R.
01:17
created

InListCondition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
dl 0
loc 69
rs 10

5 Methods

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