3 from collections import defaultdict
4 from datetime import datetime
5 from random import choices
7 from csv import DictReader
13 def __init__ (self, record_attendance=True, preferred_name_field=None):
14 self.today = str(datetime.date(datetime.now()))
15 # how much less likely should it be that a student is called upon?
17 self.record_attendance = record_attendance
20 self.__fn_studentinfo = "data/student_information.tsv"
21 self.__fn_daily_calllist = f"data/call_list-{self.today}.tsv"
22 self.__fn_daily_attendance = f"data/attendance-{self.today}.tsv"
23 self.__preferred_name_field = preferred_name_field
25 if preferred_name_field != None:
26 self.preferred_names = self.__get_preferred_names()
28 self.preferred_names = None
30 def __load_prev_questions(self):
31 previous_questions = defaultdict(int)
33 for fn in listdir("./data/"):
34 if re.match("call_list-\d{4}-\d{2}-\d{2}.tsv", fn):
35 with open(f"./data/{fn}", 'r') as f:
36 for row in DictReader(f, delimiter="\t"):
37 if not row["answered"] == "FALSE":
38 previous_questions[row["unique_name"]] += 1
40 return previous_questions
42 def __get_preferred_names(self):
43 # translate the unique name into the preferred students name,
44 # if possible, otherwise return the unique name
47 with open(self.__fn_studentinfo, 'r') as f:
48 for row in DictReader(f, delimiter="\t"):
49 row["Your UW student number"] = row[self.__preferred_name_field]
51 return(preferred_names)
53 def __get_preferred_name(self, selected_student):
54 if selected_student in self.preferred_names:
55 return self.preferred_names[selected_student]
59 def select_student_from_list(self, students_present):
60 prev_questions = self.__load_prev_questions()
62 # created a weighted list by starting out with everybody 1
63 weights = {s : 1 for s in students_present}
65 for s in students_present:
66 for i in range(0, prev_questions[s]):
67 # reduce the weight by a factor of 1/weight each time the student has been called upon
68 weights[s] = weights[s] / self.weight
70 # choose one student from the weighted list
71 # print(weights) # DEBUG LINE
72 return choices(list(weights.keys()), weights=list(weights.values()), k=1)[0]
74 def record_attendance(self, students_present):
75 # if it's the first one of the day, write it out
76 if not os.path.exists(self.__fn_daily_attendance):
77 with open(self.__fn_daily_attendance, "w") as f:
78 print("\t".join(["timestamp", "attendance_list"]), file=f)
80 # open for appending the student
81 with open(self.__fn_daily_attendance, "a") as f:
82 print("\t".join([str(datetime.now()),
83 ",".join(students_present)]),
86 def record_coldcall(self, selected_student):
87 # if it's the first one of the day, write it out
88 if not os.path.exists(self.__fn_daily_calllist):
89 with open(self.__fn_daily_calllist, "w") as f:
90 print("\t".join(["unique_name", "timestamp", "answered", "assessment"]), file=f)
92 # open for appending the student
93 with open(self.__fn_daily_calllist, "a") as f:
94 print("\t".join([selected_student, str(datetime.now()),
95 "MISSING", "MISSING"]), file=f)
97 def coldcall(self, students_present):
98 selected_student = self.select_student_from_list(students_present)
100 # record the called-upon student in the right place
101 if self.record_attendance:
102 self.record_attendance(students_present)
103 self.record_coldcall(selected_student)
105 preferred_name = self.__get_preferred_name(selected_student)
107 coldcall_message = f"{preferred_name} (@{selected_student}), you're up!"
109 coldcall_message = f"@{selected_student}, you're up!"
110 return coldcall_message
114 # test_student_list = ["jordan", "Kristen Larrick", "Madison Heisterman", "Maria.Au20", "Laura (Alia) Levi", "Leona Aklipi", "anne", "emmaaitelli", "ashleylee", "allie_partridge", "Tiana_Cole", "Hamin", "Ella Qu", "Shizuka", "Ben Baird", "Kim Do", "Isaacm24", "Sam Bell", "Courtneylg"]
115 # print(cc.coldcall(test_student_list))
117 # test_student_list = ["jordan", "Kristen Larrick", "Mako"]
118 # print(cc.coldcall(test_student_list))
120 # test_student_list = ["jordan", "Kristen Larrick"]
121 # print(cc.coldcall(test_student_list))