-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_load_data.py
More file actions
66 lines (52 loc) · 1.69 KB
/
Copy pathadd_load_data.py
File metadata and controls
66 lines (52 loc) · 1.69 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
import json
import os
import time
def initializeJson():
if not os.path.exists("data.json") or os.path.getsize("data.json") == 0:
data = {"users": []}
with open("data.json", "w") as f:
json.dump(data, f, indent=4)
def getNames():
if not os.path.exists("data.json") or os.path.getsize("data.json") == 0:
return []
with open("data.json", "r") as f:
data = json.load(f)
names = [user["name"] for user in data.get("users", [])]
return names
def addNewName(name):
with open("data.json", "r") as f:
data = json.load(f)
data["users"].append({
"name": name,
"owes": 0,
"paid": 0,
"logs": []
})
with open("data.json", "w") as f:
json.dump(data, f, indent=4)
def setPrice(target_name, new_owes):
with open("data.json", "r") as f:
data = json.load(f)
for user in data["users"]:
if user["name"] == target_name:
user["owes"] += new_owes
user["logs"].append((new_owes,time.ctime().split()[1::]))
break # stop once found
with open("data.json", "w") as f:
json.dump(data, f, indent=4)
def getPrice(target_name):
with open("data.json", "r") as f:
data = json.load(f)
for user in data["users"]:
if user["name"] == target_name:
return user["owes"]
def clearPrice(target_name):
with open("data.json","r") as f:
data = json.load(f)
for user in data["users"]:
if user["name"] == target_name:
if user["owes"] != 0:
user["paid"] = user["owes"]
user["owes"] = 0
with open("data.json", "w") as f:
json.dump(data, f, indent=4)