-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
53 lines (32 loc) · 940 Bytes
/
Copy pathdatabase.py
File metadata and controls
53 lines (32 loc) · 940 Bytes
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
# database.py
import json
import os
from constants import DATABASE_FILE
def ensure_database():
os.makedirs(os.path.dirname(DATABASE_FILE), exist_ok=True)
if not os.path.exists(DATABASE_FILE):
with open(DATABASE_FILE, "w") as f:
json.dump([], f)
def load_items():
ensure_database()
with open(DATABASE_FILE, "r") as f:
return json.load(f)
def save_items(items):
ensure_database()
with open(DATABASE_FILE, "w") as f:
json.dump(items, f, indent=4)
def add_item(item):
items = load_items()
items.append(item)
save_items(items)
def remove_item(item_id):
items = load_items()
items = [item for item in items if item["id"] != item_id]
save_items(items)
def update_item(item_id, updates):
items = load_items()
for item in items:
if item["id"] == item_id:
item.update(updates)
break
save_items(items)