]> code.communitydata.science - coldcallbot-discord.git/blob - coldcall.py
Merge branch 'master' of code.communitydata.science:coldcallbot-discord
[coldcallbot-discord.git] / coldcall.py
1 #!/usr/bin/env python3
2
3 from collections import defaultdict
4 from datetime import datetime
5 from random import choices
6 from os import listdir
7 from csv import DictReader
8
9 import os.path
10 import re
11
12 class ColdCall():
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?
16         self.weight = 2
17         self.record_attendance = record_attendance
18
19         # filenames
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
24         self.preferred_names = self.__get_preferred_names()
25         
26     def __load_prev_questions(self):
27         previous_questions = defaultdict(int)
28
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
35
36         return previous_questions
37
38     def __get_preferred_names(self):
39         # translate the unique name into the preferred students name,
40         # if possible, otherwise return the unique name
41
42         preferred_names = {}
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"]
46
47         return(preferred_names)
48         
49     def __get_preferred_name(self, selected_student):
50         if selected_student in self.preferred_names:
51             return self.preferred_names[selected_student]
52         else:
53             return None
54
55     def __select_student_from_list (self, students_present):
56         prev_questions = self.__load_prev_questions()
57         
58         # created a weighted list by starting out with everybody 1
59         weights = {s : 1 for s in students_present}
60         
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
65
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]
69
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)
75
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)]),
80                   file=f)
81
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)
87
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)
92
93     def coldcall(self, students_present):
94         selected_student = self.__select_student_from_list(students_present)
95
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)
100
101         preferred_name = self.__get_preferred_name(selected_student)
102         if preferred_name:
103             coldcall_message = f"{preferred_name} (@{selected_student}), you're up!"
104         else:
105             coldcall_message = f"@{selected_student}, you're up!"
106         return coldcall_message
107
108 # cc = ColdCall()
109
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))
112
113 # test_student_list = ["jordan", "Kristen Larrick", "Mako"]
114 # print(cc.coldcall(test_student_list))
115
116 # test_student_list = ["jordan", "Kristen Larrick"]
117 # print(cc.coldcall(test_student_list))

Community Data Science Collective || Want to submit a patch?