-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.py
41 lines (33 loc) · 1017 Bytes
/
contacts.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
import json
from dataclasses import dataclass
# ========================================================
# Contact Model
# ========================================================
@dataclass
class Contact:
# mock contacts database
db = {}
id: str
first: str
last: str
phone: str
email: str
def __init__(self, id_=None, first=None, last=None, phone=None, email=None):
self.id = id_
self.first = first
self.last = last
self.phone = phone
self.email = email
self.errors = {}
def __str__(self):
return json.dumps(self.__dict__, ensure_ascii=False)
@classmethod
def all(cls):
return list(cls.db.values())
@classmethod
def load_db(cls):
with open('contacts.json', 'r') as contacts_file:
contacts = json.load(contacts_file)
cls.db.clear()
for c in contacts:
cls.db[c['id']] = Contact(c['id'], c['first'], c['last'], c['phone'], c['email'])