-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.py
More file actions
96 lines (73 loc) · 3.18 KB
/
update.py
File metadata and controls
96 lines (73 loc) · 3.18 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
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
This module conains function for updating values of an instance
"""
import json, copy
def stringType(string: str):
try:
int(string)
return int
except ValueError:
try:
float(string)
return float
except ValueError:
return str
indent = 4
def inputTaker(Dict: dict):
global indent
for i in Dict:
arg = f'{" " * indent}{i}'
if type(Dict[i]) == list:
newList = []
while True:
item = input(f"{arg} : {Dict[i]} → ")
if item != "__end__":
item = item.strip()
if not item: # Empty input - keep old list
Dict[i] = Dict[i]
break
elif stringType(item) == int:
newList.append(int(item))
elif stringType(item) == float:
newList.append(float(item))
else:
newList.append(item)
else:
break
if newList: # Only update if new values were added
Dict[i] = newList
elif type(Dict[i]) == dict:
indent += 4
print(arg)
Dict[i] = inputTaker(Dict[i])
indent -= 4
else:
item = input(f"{arg} : {Dict[i]} → ")
if item != "__end__":
item = item.strip()
if not item: # Empty input - keep old value
continue
elif stringType(item) == int:
Dict[i] = int(item)
elif stringType(item) == float:
Dict[i] = float(item)
else:
Dict[i] = item
return Dict
def update(filePath: str, id: str):
with open(filePath, "r") as file:
data = json.load(file)
if len(data) == 0:
print("Empty file. No instances to update")
return
else:
if id in data:
instance = copy.deepcopy(data[id])
instance = inputTaker(instance)
data[id] = instance
print(f"Instance related to ID \"{id}\" updated successfully")
else:
print(f"No instance is linked to ID \"{id}\". Try again")
with open(filePath, "w") as file:
json.dump(data, file, indent=8)
return