-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent.py
194 lines (177 loc) · 4.78 KB
/
student.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import process_utils
# Constants.
# Standard University of Auckland grade boundaries.
DEFAULT_GRADE_BOUNDARIES = [
{
'grade': 'A+',
'value': 90
},
{
'grade': 'A',
'value': 85
},
{
'grade': 'A-',
'value': 80
},
{
'grade': 'B+',
'value': 75
},
{
'grade': 'B',
'value': 70
},
{
'grade': 'B-',
'value': 65
},
{
'grade': 'C+',
'value': 60
},
{
'grade': 'C',
'value': 55
},
{
'grade': 'C-',
'value': 50
},
{
'grade': 'D+',
'value': 45
},
{
'grade': 'D',
'value': 40
},
{
'grade': 'D-',
'value': 0
},
]
# Standard UoA grade ranges.
DEFAULT_UOA_GRADE_RANGES = {
'A': {
'range': 20,
'lowest': 80
},
'B': {
'range': 15,
'lowest': 65
},
'C': {
'range': 15,
'lowest': 50
},
'D': {
'range': 50,
'lowest': 15
}
}
# Class representing a single student.
class Student:
# Initialize and retrieve student data from the data string.
# @param data {string} The student data.
def __init__(self, data, prescored, test = None, position = None):
# {!Test} The test containing this question.
self.test = test
# {string} ID number.
self.id = position if position != None else 'ID not given'
# {string} Last name.
self.last_name = 'Last name'
# {string} First name.
self.first_name = 'First name'
# {string} Version of the test.
self.version = None
# {!Array<boolean>} Whether each question was right or not.
self.scores = []
# {!Array<number>} The raw answers given by the student.
self.answers = None
# {number} The student's location.
self.location = None
if prescored:
self.init_prescored(data)
else:
self.init_non_prescored(data)
def init_non_prescored(self, data):
# ID number.
self.id = data[2:11].strip()
# Last name.
self.last_name = data[12:25].strip()
# First name.
self.first_name = data[25:33].strip()
# Version of the test.
self.version = data[33:44].strip()
# Split into chunks of 2, each one is a question response.
# Convert them into integers.
# These are the raw answers given by the student.
self.answers = list(map(process_utils.int_if_possible, process_utils.split_into_chunks(data[45:], 2)))
def init_prescored(self, data):
# Split into component answers.
data = data.split(',')
# Record if each answer was correct.
self.scores = list(map(process_utils.int_if_possible, data))
# Set this student's scores, given an answer key.
def score(self, answer_key):
self.scores = []
length = len(self.answers)
for i in range(0, len(answer_key)):
if (i + 1) <= length and self.answers[i] == answer_key[i]:
self.scores.append(True)
else:
self.scores.append(False)
return
# Returns if a student was right for a question number.
def is_right(self, index):
return self.scores[index]
# Returns a numeric grade from a value (either percentage or item score) given a dictionary of grade boundaries.
def grade_key_to_grade(self, value, grade_boundaries = DEFAULT_GRADE_BOUNDARIES):
for grade in grade_boundaries:
if value >= grade['value']:
return grade['grade']
return 'D-'
# Returns the raw percentage of questions correct.
def raw_percentage(self, discarded = []):
num_correct = 0
for i in range(0, len(self.scores)):
if self.scores[i] and i not in discarded:
num_correct += 1
# Check for division by zero errors, and force the percentage to be a float.
if len(self.scores) == 0:
return 0
else:
return num_correct / ((len(self.scores) - len(discarded)) * 1.0) * 100
# Finds the location for this student..
def get_location(self):
# Calculate and cache the location.
if self.location is None:
self.test.calculate_student_stats()
return self.location
# Returns a percentage grade from a value given a dictionary of grade boundaries.
def grade_key_to_percentage(self, value, min_score, max_score, grade_boundaries = DEFAULT_GRADE_BOUNDARIES):
ranges = DEFAULT_UOA_GRADE_RANGES
# The highest and lowest values for this range.
highest = max_score
lowest = min_score
# For each grade.
for grade in grade_boundaries:
letter_grade = grade['grade']
# Check if the value is in the grade boundaries.
if value >= grade['value'] and len(letter_grade) > 1 and letter_grade[1] == '-':
lowest = grade['value']
# Set the letter grade, and lowest possible value.
# Cut the letter grade to one character.
if len(letter_grade) > 1:
letter_grade = letter_grade[0]
grade_range = highest - lowest
# Factor converting between the value range and the percentage range.
factor = ranges[letter_grade]['range'] / grade_range
part_in_grade_range = (value - lowest)
# The lowest percentage of that grade, plus the percentage * the range.
return ranges[letter_grade]['lowest'] + (factor * part_in_grade_range)
elif len(letter_grade) > 1 and letter_grade == '-':
# Change the highest boundary to the previous grade.
highest = grade['value']
return 0