/
model.py
74 lines (65 loc) · 2.18 KB
/
model.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Model module, that contains all functions, that we need to manipulate data.
"""
import configparser
from pydoc import locate
from datetime import date
config = configparser.ConfigParser()
config.read('config.ini')
backend = locate('backends.get_%s.%sSerializer' %\
(config['backend']['type'], config['backend']['type'].capitalize()))
class PressureStatistics:
"""
Class, that provides basic CRUD functionality for arterial pressure info.
"""
def __init__(self, *args, **kwargs):
"""
Initial method, that loads saved pressure statistic from file.
"""
self.backend = backend()
self.table = self.backend.read() or {}
def save(self):
"""
Sericalize and save pressure statistics to file on object deletion.
"""
self.backend.write(self.table)
def add(self, pressure_list):
"""
Add pressure value for today.
Raises exception, if we already added todays values.
"""
today = date.today().isoformat()
if today not in self.table:
self.table[today] = pressure_list
else:
raise Exception("You've already added value today.")
def update(self, date, pressure_list):
"""
Update or create new record in table.
Args:
date - date object. Points, what day we need to update.
pressure_list - list object (size=2).
"""
self.table[date.isoformat()] = pressure_list
def delete(self, date):
"""
Drops record by key.
Args:
date - date object. Points, what day we need to update.
"""
try:
del self.table[date.isoformat().split("T")[0]]
except:
raise Exception("Wrong date.")
def __str__(self):
"""
String representation for class instance.
Returns table if table exists. Otherway returns message,
that table is empty.
"""
string = ""
for date_, pressure in sorted(self.table.items()):
string += "{} - {}, {}\n".format(date_, pressure[0], pressure[1])
if not self.table:
string = "Table is empty."
return string