|
1
|
|
|
"""Defines interfaces related to various operations on table-like data.""" |
|
2
|
|
|
from abc import abstractmethod, ABC |
|
3
|
|
|
from typing import Union, Iterable |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
__all__ = ['TabularIterator', 'TabularRetriever', 'TabularMutator'] |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class TabularRetriever(ABC): |
|
10
|
|
|
"""Operations on table-like data. |
|
11
|
|
|
|
|
12
|
|
|
Classes implementing this interface gain the ability to perform various operations |
|
13
|
|
|
on data structures that resemble a table (have indexable columns, rows, etc): |
|
14
|
|
|
|
|
15
|
|
|
most importantly they can slice through the data (retrieve specific row or column) |
|
16
|
|
|
""" |
|
17
|
|
|
@abstractmethod |
|
18
|
|
|
def column(self, identifier: Union[str, int], data) -> Iterable: |
|
19
|
|
|
"""Slice though the data (table) and get the specified column's values. |
|
20
|
|
|
|
|
21
|
|
|
Args: |
|
22
|
|
|
identifier (Union[str, int]): unique identifier/index of column |
|
23
|
|
|
data (object): the data to slice through |
|
24
|
|
|
|
|
25
|
|
|
Returns: |
|
26
|
|
|
Iterable: the values contained in the column requested |
|
27
|
|
|
""" |
|
28
|
|
|
raise NotImplementedError |
|
29
|
|
|
|
|
30
|
|
|
@abstractmethod |
|
31
|
|
|
def row(self, identifier, data): |
|
32
|
|
|
"""Slice though the data (table) and get the specified row's values. |
|
33
|
|
|
|
|
34
|
|
|
Args: |
|
35
|
|
|
identifier (Union[str, int]): unique identifier/index of row |
|
36
|
|
|
data (object): the data to slice through |
|
37
|
|
|
|
|
38
|
|
|
Returns: |
|
39
|
|
|
Iterable: the values contained in the row requested |
|
40
|
|
|
""" |
|
41
|
|
|
raise NotImplementedError |
|
42
|
|
|
|
|
43
|
|
|
@abstractmethod |
|
44
|
|
|
def nb_columns(self, data) -> int: |
|
45
|
|
|
"""Get the number of columns that the data (table) have. |
|
46
|
|
|
|
|
47
|
|
|
Args: |
|
48
|
|
|
data (object): the data (table) to count its columns |
|
49
|
|
|
|
|
50
|
|
|
Returns: |
|
51
|
|
|
int: the number of the (data) table's columns |
|
52
|
|
|
""" |
|
53
|
|
|
raise NotImplementedError |
|
54
|
|
|
|
|
55
|
|
|
@abstractmethod |
|
56
|
|
|
def nb_rows(self, data) -> int: |
|
57
|
|
|
"""Get the number of rows that the data (table) have. |
|
58
|
|
|
|
|
59
|
|
|
Args: |
|
60
|
|
|
data (object): the data (table) to count its rows |
|
61
|
|
|
|
|
62
|
|
|
Returns: |
|
63
|
|
|
int: the number of the (data) table's rows |
|
64
|
|
|
""" |
|
65
|
|
|
raise NotImplementedError |
|
66
|
|
|
|
|
67
|
|
|
@abstractmethod |
|
68
|
|
|
def get_numerical_attributes(self, data) -> Iterable: |
|
69
|
|
|
r"""Get the data's attributes that represent numerical values. |
|
70
|
|
|
|
|
71
|
|
|
Returns the attributes that fall under the Numerical Variables: either |
|
72
|
|
|
Ratio or Interval type of variables. |
|
73
|
|
|
|
|
74
|
|
|
Two type of numerical variables are supported: |
|
75
|
|
|
|
|
76
|
|
|
Ratio variable: |
|
77
|
|
|
numerical variable where all operations are supported (+, -, \*, /) and true zero is defined; eg weight. |
|
78
|
|
|
|
|
79
|
|
|
Interval variable: |
|
80
|
|
|
numerical variable where differences are interpretable; supported operations: [+, -]; no true zero; |
|
81
|
|
|
eg temperature in centigrade (ie Celsius). |
|
82
|
|
|
|
|
83
|
|
|
Args: |
|
84
|
|
|
data (object): the data from which to retrieve the numerical attributes |
|
85
|
|
|
|
|
86
|
|
|
Returns: |
|
87
|
|
|
Iterable: the numerical attributes found |
|
88
|
|
|
""" |
|
89
|
|
|
raise NotImplementedError |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
class TabularIterator(ABC): |
|
93
|
|
|
"""Iterate over the rows or columns of a table-lie data structure. |
|
94
|
|
|
|
|
95
|
|
|
Classes implementing this interface gain the ability to iterate over the values |
|
96
|
|
|
found in the rows or the columns of a table-like data structure. |
|
97
|
|
|
They can also iterate over the columns indices/identifiers. |
|
98
|
|
|
""" |
|
99
|
|
|
@abstractmethod |
|
100
|
|
|
def iterrows(self, data) -> Iterable: |
|
101
|
|
|
"""Iterate over the (data) table's rows. |
|
102
|
|
|
|
|
103
|
|
|
Get an iterable over the table's rows. |
|
104
|
|
|
|
|
105
|
|
|
Args: |
|
106
|
|
|
data (object): the (data) table to iterate over its rows |
|
107
|
|
|
|
|
108
|
|
|
Returns: |
|
109
|
|
|
Iterable: the rows of the (data) table |
|
110
|
|
|
""" |
|
111
|
|
|
raise NotImplementedError |
|
112
|
|
|
|
|
113
|
|
|
@abstractmethod |
|
114
|
|
|
def itercolumns(self, data) -> Iterable: |
|
115
|
|
|
"""Iterate over the (data) table's columns. |
|
116
|
|
|
|
|
117
|
|
|
Get an iterable over the table's columns. |
|
118
|
|
|
|
|
119
|
|
|
Args: |
|
120
|
|
|
data (object): the (data) table to iterate over its columns |
|
121
|
|
|
|
|
122
|
|
|
Returns: |
|
123
|
|
|
Iterable: the columns of the (data) table |
|
124
|
|
|
""" |
|
125
|
|
|
raise NotImplementedError |
|
126
|
|
|
|
|
127
|
|
|
@abstractmethod |
|
128
|
|
|
def columnnames(self, data) -> Union[Iterable[str], Iterable[int]]: |
|
129
|
|
|
"""Iterate over data (table) column indices/identifiers. |
|
130
|
|
|
|
|
131
|
|
|
Args: |
|
132
|
|
|
data (object): the (data) table to iterate over its columns indices/identifiers |
|
133
|
|
|
|
|
134
|
|
|
Returns: |
|
135
|
|
|
Union[Iterable[str], Iterable[int]]: the column indices/identifiers of the (data) table |
|
136
|
|
|
""" |
|
137
|
|
|
raise NotImplementedError |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
class TabularMutator(ABC): |
|
141
|
|
|
"""Mutate (alter) the contents of a table-like data structure. |
|
142
|
|
|
|
|
143
|
|
|
Classes implementing this interface supply their instances the ability to alter the |
|
144
|
|
|
contents of a table-like data structure. |
|
145
|
|
|
""" |
|
146
|
|
|
@abstractmethod |
|
147
|
|
|
def add_column(self, *args, **kwargs): |
|
148
|
|
|
"""Add a new column to table-like data. |
|
149
|
|
|
|
|
150
|
|
|
Raises: |
|
151
|
|
|
NotImplementedError: [description] |
|
152
|
|
|
""" |
|
153
|
|
|
raise NotImplementedError |
|
154
|
|
|
|
|
155
|
|
|
@abstractmethod |
|
156
|
|
|
def add_columns(self, *args, **kwargs): |
|
157
|
|
|
"""Add multiple new columns to table-like data. |
|
158
|
|
|
|
|
159
|
|
|
Raises: |
|
160
|
|
|
NotImplementedError: [description] |
|
161
|
|
|
""" |
|
162
|
|
|
raise NotImplementedError |
|
163
|
|
|
|