3 from collections import defaultdict
4 from datetime import datetime
5 from random import choices
7 from csv import DictReader
14 def __init__ (self, record_attendance=True):
15 with open("configuration.json") as config_file:
16 config = json.loads(config_file.read())
18 self.today = str(datetime.date(datetime.now()))
20 # how much less likely should it be that a student is called upon?
22 self.record_attendance = record_attendance
25 self.__fn_studentinfo = config["student_info_file"]
26 self.__fn_daily_calllist = config["daily_calllist_file"].format(date=self.today)
27 self.__fn_daily_attendance = config["daily_attendance"].format(date=self.today)
29 self.unique_row = config["unique_name_rowname"]
30 if "preferred_name_rowname" in config:
31 self.preferred_row = config["preferred_name_rowname"]
33 self.preferred_row = None
35 def __load_prev_questions(self):
36 previous_questions = defaultdict(int)
38 for fn in listdir("./data/"):
39 if re.match("call_list-\d{4}-\d{2}-\d{2}.tsv", fn):
40 with open(f"./data/{fn}", 'r') as f:
41 for row in DictReader(f, delimiter="\t"):
42 if not row["answered"] == "FALSE":
43 previous_questions[row[self.unique_row]] += 1
45 return previous_questions
47 def get_preferred_names(self):
48 # translate the unique name into the preferred students name,
49 # if possible, otherwise return the unique name
52 with open(self.__fn_studentinfo, 'r') as f:
53 for row in DictReader(f, delimiter="\t"):
54 preferred_names[row[self.unique_row]] = row[self.preferred_row]
56 return(preferred_names)
58 def __get_preferred_name(self, selected_student):
59 preferred_names = self.get_preferred_names()
60 if selected_student in preferred_names:
61 return preferred_names[selected_student]
65 def select_student_from_list(self, students_present):
66 prev_questions = self.__load_prev_questions()
68 # created a weighted list by starting out with everybody 1
69 weights = {s : 1 for s in students_present}
71 for s in students_present:
72 for i in range(0, prev_questions[s]):
73 # reduce the weight by a factor of 1/weight each time the student has been called upon
74 weights[s] = weights[s] / self.weight
76 # choose one student from the weighted list
77 # print(weights) # DEBUG LINE
78 return choices(list(weights.keys()), weights=list(weights.values()), k=1)[0]
80 def record_attendance(self, students_present):
81 # if it's the first one of the day, write it out
82 if not os.path.exists(self.__fn_daily_attendance):
83 with open(self.__fn_daily_attendance, "w") as f:
84 print("\t".join(["timestamp", "attendance_list"]), file=f)
86 # open for appending the student
87 with open(self.__fn_daily_attendance, "a") as f:
88 print("\t".join([str(datetime.now()),
89 ",".join(students_present)]),
92 def record_coldcall(self, selected_student):
93 # if it's the first one of the day, write it out
94 if not os.path.exists(self.__fn_daily_calllist):
95 with open(self.__fn_daily_calllist, "w") as f:
96 print("\t".join([self.unique_row, self.preferred_row, "answered", "assessment", "timestamp"]), file=f)
98 preferred_name = self.__get_preferred_name(selected_student)
99 if preferred_name == None:
102 # open for appending the student
103 with open(self.__fn_daily_calllist, "a") as f:
104 print("\t".join([selected_student, preferred_name,
105 "MISSING", "MISSING", str(datetime.now())]), file=f)
107 def coldcall(self, students_present):
108 selected_student = self.select_student_from_list(students_present)
110 # record the called-upon student in the right place
111 if self.record_attendance:
112 self.record_attendance(students_present)
113 self.record_coldcall(selected_student)
115 preferred_name = self.__get_preferred_name(selected_student)
117 coldcall_message = f"{preferred_name} (@{selected_student}), you're up!"
119 coldcall_message = f"@{selected_student}, you're up!"
120 return coldcall_message