-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
51 lines (42 loc) · 1.11 KB
/
database.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
import json
from event import Event
from typing import Optional
from datetime import date
with open('events.json', 'w+') as f:
x = f.read()
if x == '': x = '[]'
db = json.loads(x)
def store_event(evt: Event):
newitem = {
'type': 'event',
'id': evt.id,
'name': evt.name,
'creator': evt.creator_email,
'date': evt.date.isoformat(),
'fips': evt.fips,
'seats': evt.seats
}
stored = False
for i, item in enumerate(db):
if item['id'] == evt.id:
db[i] = newitem
stored = True
break
if not stored: db.append(newitem)
with open('events.json', 'w') as f:
json.dump(db, f)
def load_event(_id: str) -> Optional[Event]:
for item in db:
if item['id'] == _id:
return parse_event(item)
def parse_event(obj: dict) -> Event:
return Event(
id=obj['id'],
name=obj['name'],
creator_email=obj['creator'],
date=date.fromisoformat(obj['date']),
fips=obj['fips'],
seats=obj['seats']
)
def num_events() -> int:
return len(db)