-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_db.py
105 lines (74 loc) · 2.6 KB
/
json_db.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json
from pathlib import Path
from typing import TypeVar, Any, cast
TFallback = TypeVar('TFallback')
NO_FALLBACK = object()
class JSONDatabase:
"""
A very simple JSON file-based database.
"""
def __init__(self, filename: str):
self.filename: str = filename
self.data: dict[str, dict[str, Any]] = self._load()
self.dirty: bool = False
def _load(self) -> dict[str, dict[str, Any]]:
"""
Load the database from the file.
"""
with open(self.filename, 'rt', encoding='utf-8') as f:
data = json.load(f)
return data
def save(self):
"""
Save the database to the file.
"""
if self.data is None:
raise Exception("No data to save.")
if not self.dirty:
return
# Convert to JSON first before we touch the file (safer if there are errors)
data = json.dumps(self.data, indent=4, separators=(',', ': '), sort_keys=True)
# Write the data
Path(self.filename).write_text(data, encoding='utf-8')
self.dirty = False
def mark_dirty(self):
"""
Mark the database as dirty, so it will be saved in future.
"""
self.dirty = True
def __getitem__(self, id: str) -> dict[str, Any]:
return self.data[str(id)]
def get(self, id: str, fallback:TFallback=NO_FALLBACK) -> dict[str, Any]|TFallback:
"""
Get the value of an item, or return a fallback value if it doesn't exist.
"""
try:
return self.data[str(id)]
except KeyError:
if fallback is NO_FALLBACK:
raise
return fallback
def __setitem__(self, id: str, value: dict[str, Any]) -> dict[str, Any]:
self.data[str(id)] = value
self.mark_dirty()
return value
def update(self, id: str, update_values: dict[str, Any]) -> dict[str, Any]:
"""
Update the value of an item.
"""
old_values = self.data[str(id)]
new_values = {**old_values, **update_values}
self.data[str(id)] = new_values
self.mark_dirty()
return update_values
if __name__ == '__main__':
from typing import Any
db = JSONDatabase('database.json')
db['1234'] = dict(name='Dirty John', age=42)
db.save()
db = JSONDatabase('database.json')
print(db['1234'])
db = JSONDatabase('database.json')
db.update('1234', dict(age=43))
db.update('1234', dict(gender='m'))
db.save()