-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
44 lines (30 loc) · 1.3 KB
/
database.py
File metadata and controls
44 lines (30 loc) · 1.3 KB
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
import json
from config import bot_config
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Database:
__data = {}
def __init__(self, filename: str):
self.__filename = filename
try:
with open(filename, "r") as db_file:
try:
self.__data = json.load(db_file)
print(f"Read data from db `{filename}`")
except ValueError:
print(f"Db `{filename}` is empty or incorrect. Assigned null to data.")
except FileNotFoundError:
print(f"Db `{filename}` doesn't exist. Created it.")
with open(filename, "x") as db_file:
pass
def change_characteristic(self, username: str, characteristic: str, value: str) -> None:
try:
self.__data[username].append({characteristic: value})
except:
self.__data[username] = [{characteristic: value}]
def receive_characteristics(self, username: str) -> str:
return json.dumps(self.__data[username], indent = 4)
def save_database(self):
with open(self.__filename, "w") as db_file:
json.dump(self.__data, db_file)
db = Database(bot_config.db_filename)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~