-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_load_test.py
72 lines (45 loc) · 1.36 KB
/
save_load_test.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
#
import json
data = {
"username": "logix",
"active": True,
"num": 8843.89,
"id": 42,
}
# ---------------------------- #
def save_all(): # Save ALL data
with open("data.json", "w") as f1:
json.dump(data, f1, indent=4)
f1.close()
print(data) # DEBUG
# ---------------------------- #
def save(key: str, val: any): # Save SOME data
data[key] = val # Assign value and key to change
with open("data.json", "w") as f2: # Open file
json.dump(data, f2, indent=4) # Add data
f2.close() # Close file
# --------------------------------- #
def load_all(): # Loading ALL data
global data
with open("data.json") as f3:
getdata = json.load(f3)
data = getdata
f3.close()
print(data) # DEBUG
# ---------------------------- #
def load(key: str): # Load SOME data
with open("data.json") as f4:
ldata = json.load(f4) # load the file
val = ldata[key] # assign val from loaded key
f4.close() # close the file
print(val) # DEBUG
""" ==================================================== """
def main():
save_all()
print("")
save("num", 777)
load("username")
load("id")
load("num")
if __name__ == '__main__':
main()