-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.py
30 lines (24 loc) · 1.12 KB
/
table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""This is a part of an exercise from https://exercism.io/my/tracks/python"""
class Table(dict):
"""A table is a row-column matrix where each row is indexed using the
`index` column. A table is initialized using a list of rows, where each row
is a dictionnary maping column names to their respective values. (Note that
no verification of the rows shape is made, we trust you on this one :D).
"""
def __init__(self, table_name, elements, index):
self.name = table_name
self.elements = {entry[index]: entry for entry in elements}
super().__init__(self.elements)
self.index = index
def __getitem__(self, key):
return self.elements[key]
def __setitem__(self, key, value):
self.elements[key] = value
def __repr__(self):
return str(self.elements)
def add_row(self, entry):
"""Add a row in the table. The `entry` argument is a dictionnary maping
column names to their respective values. (Note that no verification of
the rows shape is made, we trust you on this one :D).
"""
self.elements[entry[self.index]] = entry