3 from collections import defaultdict
4 from datetime import datetime
5 from random import choices
7 from csv import DictReader
15 self.today = str(datetime.date(datetime.now()))
16 # how much less likely should it be that a student is called upon?
20 self.__fn_studentinfo = "data/student_information.tsv"
21 self.__fn_daily_calllist = f"data/call_list-{self.today}.tsv"
22 self.__fn_daily_attendence = f"data/attendence-{self.today}.tsv"
24 def __load_prev_questions(self):
25 previous_questions = defaultdict(int)
27 for fn in listdir("./data/"):
28 if re.match("call_list-\d{4}-\d{2}-\d{2}.tsv", fn):
29 with open(f"./data/{fn}", 'r') as f:
30 for row in DictReader(f, delimiter="\t"):
31 previous_questions[row["discord_name"]] += 1
33 return previous_questions
35 def __get_preferred_name(self, selected_student):
36 # translate the discord name into the preferred students name,
37 # if possible, otherwise return the discord name
40 with open(self.__fn_studentinfo, 'r') as f:
41 for line in f.readlines():
42 x = line.strip().split("\t")
43 if x[0] == "Timestamp":
46 preferred_names[x[4]] = x[2]
48 if selected_student in preferred_names:
49 return preferred_names[selected_student]
53 def __select_student_from_list (self, students_present):
54 prev_questions = self.__load_prev_questions()
56 # created a weighted list by starting out with everybody 1
57 weights = {s : 1 for s in students_present}
59 for s in students_present:
60 for i in range(0, prev_questions[s]):
61 # reduce the weight by a factor of 1/weight each time the student has been called upon
62 weights[s] = weights[s] / self.weight
64 # choose one student from the weighted list
66 return choices(list(weights.keys()), weights=list(weights.values()), k=1)[0]
68 def __record_attendence(self, students_present):
69 # if it's the first one of the day, write it out
70 if not os.path.exists(self.__fn_daily_attendence):
71 with open(self.__fn_daily_attendence, "w") as f:
72 print("\t".join(["timestamp", "attendence_list"]), file=f)
74 # open for appending the student
75 with open(self.__fn_daily_attendence, "a") as f:
76 print("\t".join([str(datetime.now()),
77 ",".join(students_present)]),
80 def __record_coldcall(self, selected_student):
81 # if it's the first one of the day, write it out
82 if not os.path.exists(self.__fn_daily_calllist):
83 with open(self.__fn_daily_calllist, "w") as f:
84 print("\t".join(["discord_name", "timestamp", "answered", "assessment"]), file=f)
86 # open for appending the student
87 with open(self.__fn_daily_calllist, "a") as f:
88 print("\t".join([selected_student, str(datetime.now()),
89 "MISSING", "MISSING"]), file=f)
91 def coldcall(self, students_present):
92 selected_student = self.__select_student_from_list(students_present)
94 # record the called-upon student in the right place
95 self.__record_attendence(students_present)
96 self.__record_coldcall(selected_student)
98 preferred_name = self.__get_preferred_name(selected_student)
100 coldcall_message = f"{preferred_name} (@{selected_student}), you're up!"
102 coldcall_message = f"@{selected_student}, you're up!"
103 return coldcall_message
107 # test_student_list = ["Jordan", "Kristen Larrick", "Madison Heisterman", "Maria.Au20", "Laura (Alia) Levi", "Leona Aklipi", "Linya Feng", "anne", "kirst", "emmaaitelli", "ashleylee", "allie_partridge", "Tiana_Cole", "Hamin", "Ella Qu", "Angel Su", "Shizuka", "Ben Baird", "Kim Do", "Isaacm24", "Sam Bell", "Courtneylg", "TasnimHasan"]
108 # cc.coldcall(test_student_list)
110 # test_student_list = ["Jordan", "Kristen Larrick", "Mako"]
111 # cc.coldcall(test_student_list)
113 # test_student_list = ["Jordan", "Kristen Larrick"]
114 # cc.coldcall(test_student_list)