-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathtestctl.py
executable file
·85 lines (68 loc) · 2.24 KB
/
mathtestctl.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
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python3
import argparse
import os
import sys
import json
from dataclasses import dataclass
__version__ = "1.1"
@dataclass
class MathTestStats:
num_wrong: int
num_right: int
num_fail: int
def __str__(self):
return f"{self.num_wrong} wrong, {self.num_right} right, {self.num_fail} failed"
def reset(self):
print("Nuh uh lil bro")
def save(self):
stats_dict = {"w": self.num_wrong, "r": self.num_right, "f": self.num_fail}
with open("./mts.json", "w", encoding="utf8") as mts_json:
json.dump(stats_dict, mts_json)
def set_wd():
script_path = os.path.abspath(__file__)
script_dir = os.path.dirname(script_path)
os.chdir(script_dir)
def load_current_stats():
if not os.path.exists("./mts.json"):
return MathTestStats(0, 0, 0)
with open("./mts.json", "r", encoding="utf8") as mts_json:
mts = json.load(mts_json)
return MathTestStats(int(mts["w"]), int(mts["r"]), int(mts["f"]))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"action", type=str, choices=["addwrong", "addright", "addfail", "view", "reset"]
)
parser.add_argument("--number", "-n", type=int)
parser.add_argument(
"--version",
"-V",
action="version",
version=f"%(prog)s {__version__}",
help="display version and exit",
)
args = parser.parse_args()
set_wd()
current_stats = load_current_stats()
if args.number is not None and args.number < 0:
print("That would be cheating. Get lost.")
if args.action == "view":
print(current_stats)
elif args.action == "reset":
current_stats.reset()
elif args.action == "addwrong":
if args.number is None:
print("please pass number")
sys.exit(1)
current_stats.num_wrong += args.number
if args.number is None:
print("please pass number")
sys.exit(1)
elif args.action == "addright":
if args.number is None:
print("please pass number")
sys.exit(1)
current_stats.num_right += args.number
elif args.action == "addfail":
current_stats.num_fail += args.number
current_stats.save()