-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
37 lines (27 loc) · 914 Bytes
/
models.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
import json
from datetime import datetime
class Diary:
FILENAME = 'diary.json'
entries = []
def build_entries(self):
return ''.join([f'{entry.get("created")}:\n{entry.get("description")}\n---\n' for entry in self.entries])
def load(self):
try:
with open(self.FILENAME, 'r') as f:
self.entries = json.loads(f.read())
except FileNotFoundError:
return []
def save(self):
with open(self.FILENAME, 'w') as f:
f.write(json.dumps(self.entries))
def add_entry(self, entry):
self.entries.append(entry.serialize())
class Entry:
def __init__(self, description):
self.description = description
self.created = datetime.now().strftime("%c")
def serialize(self):
return {
"description": self.description,
"created": self.created
}