-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatautils.py
More file actions
36 lines (29 loc) · 873 Bytes
/
datautils.py
File metadata and controls
36 lines (29 loc) · 873 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
import json
import os.path
class Data(dict):
def __init__(self, file: str, delay_write=False):
super().__init__()
self.delay_write = delay_write
self.file = file
if os.path.exists(self.file):
self.read()
else:
self.write()
def __getitem__(self, y):
if y not in self.keys():
return None
return super().__getitem__(y)
def __setitem__(self, key, value):
super().__setitem__(key, value)
if not self.delay_write:
self.write()
def __delitem__(self, key):
super().__delitem__(key)
if not self.delay_write:
self.write()
def read(self):
with open(self.file, 'r') as f:
self.update(json.load(f))
def write(self):
with open(self.file, 'w') as f:
json.dump(self, f)