-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanner.py
63 lines (56 loc) · 2.16 KB
/
planner.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
# planner.py
# 02.19.2022
import json
def writeJSON(filename, data):
with open(filename, 'w') as outfile:
json.dump(data, outfile, indent=4,sort_keys=True)
return True
def readJSON(filename):
with open(filename, 'r') as infile:
data = json.load(infile)
return data
def add_course(user, course, code, semester):
plan_json = readJSON('planner.json')
if user in plan_json:
if semester in plan_json[user]:
plan_json[user][semester].append(course + " " + code)
else:
plan_json[user][semester] = [course + " " + code]
else:
plan_json[user] = {semester: [course + " " + code]}
writeJSON('planner.json', plan_json)
def remove_course(user, course, code):
plan_json = readJSON('planner.json')
if user not in plan_json:
return "You have no registered courses"
for key, value in plan_json[user].items():
for i in value:
if i == course + " " + code:
plan_json[user][key].remove(i)
writeJSON('planner.json', plan_json)
return "Course successfully removed"
return "Was unable to find course"
def move_course(user, course, code, oldsem, newsem):
plan_json = readJSON('planner.json')
if user not in plan_json:
return "You have no registered courses"
add_course(user, course, code, newsem)
if oldsem in plan_json[user]:
if course + " " + code in plan_json[user][oldsem]:
plan_json[user][oldsem].remove(course + " " + code)
writeJSON('planner.json', plan_json)
return f"Successfully moved {course} {code}"
else:
return f"Could not find {course} {code} in {oldsem}. Added it to {newsem} anyway"
return f"Could not find {oldsem} in your planner lol. Added course to {newsem} anyway"
def planner(user):
leader = "```\n"
plan_json = readJSON('planner.json')
if user not in plan_json:
return "You have no registered courses"
for key,value in plan_json[user].items():
if value != []:
leader += f"{key}\n"
for i in value:
leader += "- " + i + "\n"
return leader + "```"