-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAO.py
39 lines (30 loc) · 826 Bytes
/
DAO.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
from abc import ABC
import pickle
class DAO(ABC):
def __init__(self, datasource: str):
self.datasource = datasource
self.cache = {}
try:
self.load()
except FileNotFoundError:
self.dump()
def dump(self):
pickle.dump(self.cache, open(self.datasource, 'wb'))
def load(self):
self.cache = pickle.load(open(self.datasource, 'rb'))
def add(self, obj):
self.cache[obj.codigo] = obj
self.dump()
def get(self, key):
try:
return self.cache[key]
except KeyError:
return False
def remove(self, key):
try:
self.cache.pop(key)
self.dump()
except KeyError:
return False
def get_all(self):
return self.cache.values()