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

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