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):
 
  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"
 
  24         self.preferred_names = self.__get_preferred_names()
 
  26     def __load_prev_questions(self):
 
  27         previous_questions = defaultdict(int)
 
  29         for fn in listdir("./data/"):
 
  30             if re.match("call_list-\d{4}-\d{2}-\d{2}.tsv", fn):
 
  31                 with open(f"./data/{fn}", 'r') as f:
 
  32                     for row in DictReader(f, delimiter="\t"):
 
  33                         if not row["answered"] == "FALSE":
 
  34                             previous_questions[row["unique_name"]] += 1
 
  36         return previous_questions
 
  38     def __get_preferred_names(self):
 
  39         # translate the unique name into the preferred students name,
 
  40         # if possible, otherwise return the unique name
 
  43         with open(self.__fn_studentinfo, 'r') as f:
 
  44             for row in DictReader(f, delimiter="\t"):
 
  45                 preferred_names[row["Your username on the class Teams server"]] = row["Name you'd like to go by in class"]
 
  47         return(preferred_names)
 
  49     def __get_preferred_name(self, selected_student):
 
  50         if selected_student in self.preferred_names:
 
  51             return self.preferred_names[selected_student]
 
  55     def __select_student_from_list (self, students_present):
 
  56         prev_questions = self.__load_prev_questions()
 
  58         # created a weighted list by starting out with everybody 1
 
  59         weights = {s : 1 for s in students_present}
 
  61         for s in students_present:
 
  62             for i in range(0, prev_questions[s]):
 
  63                 # reduce the weight by a factor of 1/weight each time the student has been called upon
 
  64                 weights[s] = weights[s] / self.weight
 
  66         # choose one student from the weighted list
 
  67         # print(weights) # DEBUG LINE
 
  68         return choices(list(weights.keys()), weights=list(weights.values()), k=1)[0]
 
  70     def __record_attendance(self, students_present):
 
  71         # if it's the first one of the day, write it out
 
  72         if not os.path.exists(self.__fn_daily_attendance):
 
  73             with open(self.__fn_daily_attendance, "w") as f:
 
  74                 print("\t".join(["timestamp", "attendance_list"]), file=f)
 
  76         # open for appending the student
 
  77         with open(self.__fn_daily_attendance, "a") as f:
 
  78             print("\t".join([str(datetime.now()),
 
  79                              ",".join(students_present)]),
 
  82     def __record_coldcall(self, selected_student):
 
  83         # if it's the first one of the day, write it out
 
  84         if not os.path.exists(self.__fn_daily_calllist):
 
  85             with open(self.__fn_daily_calllist, "w") as f:
 
  86                 print("\t".join(["unique_name", "timestamp", "answered", "assessment"]), file=f)
 
  88         # open for appending the student
 
  89         with open(self.__fn_daily_calllist, "a") as f:
 
  90             print("\t".join([selected_student, str(datetime.now()),
 
  91                              "MISSING", "MISSING"]), file=f)
 
  93     def coldcall(self, students_present):
 
  94         selected_student = self.__select_student_from_list(students_present)
 
  96         # record the called-upon student in the right place
 
  97         if self.record_attendance:
 
  98             self.__record_attendance(students_present)
 
  99         self.__record_coldcall(selected_student)
 
 101         preferred_name = self.__get_preferred_name(selected_student)
 
 103             coldcall_message = f"{preferred_name} (@{selected_student}), you're up!"
 
 105             coldcall_message = f"@{selected_student}, you're up!"
 
 106         return coldcall_message
 
 110 # 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"]
 
 111 # print(cc.coldcall(test_student_list))
 
 113 # test_student_list = ["jordan", "Kristen Larrick", "Mako"]
 
 114 # print(cc.coldcall(test_student_list))
 
 116 # test_student_list = ["jordan", "Kristen Larrick"]
 
 117 # print(cc.coldcall(test_student_list))