]> code.communitydata.science - coldcallbot-discord.git/blob - coldcall.py
work to build a much better manual version of the script
[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, 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?
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         self.__preferred_name_field = preferred_name_field
24
25         if preferred_name_field != None:
26             self.preferred_names = self.__get_preferred_names()
27         else:
28             self.preferred_names = None
29         
30     def __load_prev_questions(self):
31         previous_questions = defaultdict(int)
32
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
39
40         return previous_questions
41
42     def __get_preferred_names(self):
43         # translate the unique name into the preferred students name,
44         # if possible, otherwise return the unique name
45
46         preferred_names = {}
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]
50
51         return(preferred_names)
52         
53     def __get_preferred_name(self, selected_student):
54         if selected_student in self.preferred_names:
55             return self.preferred_names[selected_student]
56         else:
57             return None
58
59     def select_student_from_list(self, students_present):
60         prev_questions = self.__load_prev_questions()
61         
62         # created a weighted list by starting out with everybody 1
63         weights = {s : 1 for s in students_present}
64         
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
69
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]
73
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)
79
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)]),
84                   file=f)
85
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)
91
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)
96
97     def coldcall(self, students_present):
98         selected_student = self.select_student_from_list(students_present)
99
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)
104
105         preferred_name = self.__get_preferred_name(selected_student)
106         if preferred_name:
107             coldcall_message = f"{preferred_name} (@{selected_student}), you're up!"
108         else:
109             coldcall_message = f"@{selected_student}, you're up!"
110         return coldcall_message
111
112 # cc = ColdCall()
113
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))
116
117 # test_student_list = ["jordan", "Kristen Larrick", "Mako"]
118 # print(cc.coldcall(test_student_list))
119
120 # test_student_list = ["jordan", "Kristen Larrick"]
121 # print(cc.coldcall(test_student_list))

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