]> code.communitydata.science - coldcallbot-discord.git/blob - coldcall.py
initial version of commit updating coldcall.py
[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 import json
12
13 class ColdCall():
14     def __init__ (self, record_attendance=True):
15         with open("configuration.json") as config_file:
16             config = json.loads(config_file.read())
17
18         self.today = str(datetime.date(datetime.now()))
19         # how much less likely should it be that a student is called upon?
20         self.weight = 2
21         self.record_attendance = record_attendance
22
23         # filenames
24         self.__fn_studentinfo = config["student_info_file"]
25         self.__fn_daily_calllist = config["daily_calllist_file"].format(date=self.today)
26         self.__fn_daily_attendance = config["daily_attendance"].format(date=self.today)
27
28         self.unique_row = config["unique_name_rowname"]
29         self.preferred_row = config["preferred_name_rowname"]
30
31         self.preferred_names = self.__get_preferred_names()
32         
33     def __load_prev_questions(self):
34         previous_questions = defaultdict(int)
35
36         for fn in listdir("./data/"):
37             if re.match("call_list-\d{4}-\d{2}-\d{2}.tsv", fn):
38                 with open(f"./data/{fn}", 'r') as f:
39                     for row in DictReader(f, delimiter="\t"):
40                         if not row["answered"] == "FALSE":
41                             previous_questions[row[self.unique_row]] += 1
42
43         return previous_questions
44
45     def __get_preferred_names(self):
46         # translate the unique name into the preferred students name,
47         # if possible, otherwise return the unique name
48
49         preferred_names = {}
50         with open(self.__fn_studentinfo, 'r') as f:
51             for row in DictReader(f, delimiter="\t"):
52                 preferred_names[row[self.unique_row]] = row[self.preferred_row]
53
54         return(preferred_names)
55         
56     def __get_preferred_name(self, selected_student):
57         if selected_student in self.preferred_names:
58             return self.preferred_names[selected_student]
59         else:
60             return None
61
62     def __select_student_from_list (self, students_present):
63         prev_questions = self.__load_prev_questions()
64         
65         # created a weighted list by starting out with everybody 1
66         weights = {s : 1 for s in students_present}
67         
68         for s in students_present:
69             for i in range(0, prev_questions[s]):
70                 # reduce the weight by a factor of 1/weight each time the student has been called upon
71                 weights[s] = weights[s] / self.weight
72
73         # choose one student from the weighted list
74         # print(weights) # DEBUG LINE
75         return choices(list(weights.keys()), weights=list(weights.values()), k=1)[0]
76
77     def __record_attendance(self, students_present):
78         # if it's the first one of the day, write it out
79         if not os.path.exists(self.__fn_daily_attendance):
80             with open(self.__fn_daily_attendance, "w") as f:
81                 print("\t".join(["timestamp", "attendance_list"]), file=f)
82
83         # open for appending the student
84         with open(self.__fn_daily_attendance, "a") as f:
85             print("\t".join([str(datetime.now()),
86                              ",".join(students_present)]),
87                   file=f)
88
89     def __record_coldcall(self, selected_student):
90         # if it's the first one of the day, write it out
91         if not os.path.exists(self.__fn_daily_calllist):
92             with open(self.__fn_daily_calllist, "w") as f:
93                 print("\t".join([self.unique_row, self.preferred_row, "answered", "assessment", "timestamp"]), file=f)
94
95         preferred_name = self.__get_preferred_name(selected_student)
96
97         # open for appending the student
98         with open(self.__fn_daily_calllist, "a") as f:
99             print("\t".join([selected_student, preferred_name,
100                              "MISSING", "MISSING", str(datetime.now())]), file=f)
101
102     def coldcall(self, students_present):
103         selected_student = self.__select_student_from_list(students_present)
104
105         # record the called-upon student in the right place
106         if self.record_attendance:
107             self.__record_attendance(students_present)
108         self.__record_coldcall(selected_student)
109
110         preferred_name = self.__get_preferred_name(selected_student)
111         if preferred_name:
112             coldcall_message = f"{preferred_name} (@{selected_student}), you're up!"
113         else:
114             coldcall_message = f"@{selected_student}, you're up!"
115         return coldcall_message
116

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