]> code.communitydata.science - coldcallbot-discord.git/blob - coldcall.py
4987c741978a1826c4505a74eae90808251f1494
[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 discord
12
13 class ColdCall():
14     def __init__ (self):
15         self.today = str(datetime.date(datetime.now()))
16         # how much less likely should it be that a student is called upon?
17         self.weight = 2 
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     def __load_prev_questions(self):
25         previous_questions = defaultdict(int)
26
27         for fn in listdir("./data/"):
28             if re.match("call_list-\d{4}-\d{2}-\d{2}.tsv", fn):
29                 with open(f"./data/{fn}", 'r') as f:
30                     for row in DictReader(f, delimiter="\t"):
31                         previous_questions[row["discord_name"]] += 1
32
33         return previous_questions
34     
35     def __get_preferred_name(self, selected_student):
36         # translate the discord name into the preferred students name,
37         # if possible, otherwise return the discord name
38
39         preferred_names = {}
40         with open(self.__fn_studentinfo, 'r') as f:
41             for row in DictReader(f, delimiter="\t"):
42                 preferred_names[row["Your username on the class Discord server"]] = row["Name you'd like to go by in class"]
43
44         if selected_student in preferred_names:
45             return preferred_names[selected_student]
46         else:
47             return None
48
49     def __select_student_from_list (self, students_present):
50         prev_questions = self.__load_prev_questions()
51         
52         # created a weighted list by starting out with everybody 1
53         weights = {s : 1 for s in students_present}
54         
55         for s in students_present:
56             for i in range(0, prev_questions[s]):
57                 # reduce the weight by a factor of 1/weight each time the student has been called upon
58                 weights[s] = weights[s] / self.weight
59
60         # choose one student from the weighted list
61         print(weights)
62         return choices(list(weights.keys()), weights=list(weights.values()), k=1)[0]
63
64     def __record_attendance(self, students_present):
65         # if it's the first one of the day, write it out
66         if not os.path.exists(self.__fn_daily_attendance):
67             with open(self.__fn_daily_attendance, "w") as f:
68                 print("\t".join(["timestamp", "attendance_list"]), file=f)
69
70         # open for appending the student
71         with open(self.__fn_daily_attendance, "a") as f:
72             print("\t".join([str(datetime.now()),
73                              ",".join(students_present)]),
74                   file=f)
75
76     def __record_coldcall(self, selected_student):
77         # if it's the first one of the day, write it out
78         if not os.path.exists(self.__fn_daily_calllist):
79             with open(self.__fn_daily_calllist, "w") as f:
80                 print("\t".join(["discord_name", "timestamp", "answered", "assessment"]), file=f)
81
82         # open for appending the student
83         with open(self.__fn_daily_calllist, "a") as f:
84             print("\t".join([selected_student, str(datetime.now()),
85                              "MISSING", "MISSING"]), file=f)
86
87     def coldcall(self, students_present):
88         selected_student = self.__select_student_from_list(students_present)
89
90         # record the called-upon student in the right place
91         self.__record_attendance(students_present)
92         self.__record_coldcall(selected_student)
93
94         preferred_name = self.__get_preferred_name(selected_student)
95         if preferred_name:
96             coldcall_message = f"{preferred_name} (@{selected_student}), you're up!"
97         else:
98             coldcall_message = f"@{selected_student}, you're up!"
99         return coldcall_message
100
101 # cc = ColdCall()
102  
103 # test_student_list = ["jordan", "Kristen Larrick", "Madison Heisterman", "Maria.Au20", "Laura (Alia) Levi", "Leona Aklipi", "Linya Feng", "anne", "kirst", "emmaaitelli", "ashleylee", "allie_partridge", "Tiana_Cole", "Hamin", "Ella Qu", "Angel Su", "Shizuka", "Ben Baird", "Kim Do", "Isaacm24", "Sam Bell", "Courtneylg", "TasnimHasan"]
104 # print(cc.coldcall(test_student_list))
105
106 # test_student_list = ["jordan", "Kristen Larrick", "Mako"]
107 # print(cc.coldcall(test_student_list))
108
109 # test_student_list = ["jordan", "Kristen Larrick"]
110 # print(cc.coldcall(test_student_list))

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