Repository navigation
Expand file tree
/
Copy pathDictExample.py
More file actions
83 lines (66 loc) · 2.29 KB
/
Copy pathDictExample.py
File metadata and controls
83 lines (66 loc) · 2.29 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
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
from DataManager import *
import os
class ExampleDataFields (DataFields):
STRING = "string"
NUMBER = "number"
BOOL = "bool"
pass
class ExampleDataManager (DataManager):
def __init__(self):
super().__init__(os.path.join(os.path.dirname(__file__), "data"), self.DatabaseType.DICT) # Forced the datapath here.
def getDefaultValues(self) -> dict:
return {
ExampleDataFields.STRING: "example",
ExampleDataFields.NUMBER: 1234,
ExampleDataFields.BOOL: True
}
# Code from here point on.
breakpoint = lambda: input("\nPress Enter to continue. \n")
manager = ExampleDataManager()
identifier = input("Enter identifier: ")
if not manager.datafileExists(identifier):
manager.createDatafile(identifier)
breakpoint()
# Get the data for "user123"
user_data = manager.getData(identifier)
print("User Data:", user_data)
breakpoint()
# Access the specific data key (e.g., STRING, NUMBER, BOOL)
user_string = manager.getData(identifier, ExampleDataFields.STRING)
print("User String:", user_string)
breakpoint()
# Set a new value for the STRING field
manager.setData(identifier, ExampleDataFields.STRING, "new_value_for_string")
updated_user_data = manager.getData(identifier)
print("Updated User Data:", updated_user_data)
breakpoint()
# Attempt to fetch a key that does not exist (will raise error)
try:
invalid_key = manager.getData(identifier, "non_existent_key")
except DataManagerErrors.DataKeyNotExists as e:
print("noKey Error:", e)
breakpoint()
# Attempt to create a database for an existing identifier (will raise error)
try:
manager.createDatafile(identifier)
except DataManagerErrors.PathExists as e:
print("repeatID Error:", e)
breakpoint()
# Attempt to delete the created database
manager.deleteDatafile(identifier)
# Attempt to access the deleted database
try:
manager.getData(identifier)
except DataManagerErrors.PathNotExists as e:
print("delete Error:", e)
breakpoint()
# Delete the entire database folder (...DataAPI\data). Once when it is still alive, once again after it is deleted.
while True:
try:
manager.DANGER_DELETEALL()
except DataManagerErrors.PathNotExists as e:
print("deleteall Error:", e)
break
except Exception as e:
print("deleteall Error:", e)
break