Skip to content

Commit dfd1390

Browse files
authored
Extract file trimming function and use in MCTA key (#54) (#55)
Fixes #49 by trimming empty values off the end of answer keys before writing to file.
1 parent 28632dd commit dfd1390

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

code/data_exporting.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,9 @@ def clean_up(self, replace_empty_with: str = ""):
150150
"""Removes the extra headings from the heading row and replaces blank
151151
cells with `replace_empty_with`. Pads any short rows with that value to
152152
make all rows the same length. """
153-
# Finds the length of the longest row by subtracting the smallest index
154-
# of the first element in a row that is not blank from the length of the
155-
# header row.
153+
# Finds the length of the longest row by subtracting the minimum number of trailing empty elements
156154
longest_length = len(self.data[0]) - min([
157-
next((i for i, ans in enumerate(reversed(row)) if ans != ""), 0)
155+
list_utils.count_trailing_empty_elements(row)
158156
for row in self.data[1:]
159157
])
160158
self.data[0] = self.data[0][:longest_length]

code/list_utils.py

+5
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,8 @@ def transpose(matrix: tp.List[tp.List[T]]) -> tp.List[tp.List[T]]:
130130
"Input matrix rows must all have the same length for transposing.")
131131
return [[row[col_index] for row in matrix]
132132
for col_index in range(len(matrix[0]))]
133+
134+
135+
def count_trailing_empty_elements(items: tp.List[tp.Any]) -> int:
136+
"""Returns the number of trailing empty elements in the list."""
137+
return next((i for i, x in enumerate(reversed(items)) if x != ""), len(items))

code/mcta_processing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import itertools
55

66
from data_exporting import format_timestamp_for_file, save_csv, OutputSheet
7+
import list_utils
78

89
"""Support for additional outputs used by the Multiple Choice Test Analysis software."""
910

@@ -70,8 +71,9 @@ def build_key_csv(answers: tp.List[str]) -> tp.List[tp.List[str]]:
7071
Params:
7172
answers: All of the answers for this form code, in order.
7273
"""
74+
length = len(answers) - list_utils.count_trailing_empty_elements(answers)
7375
header = ["", "Answer", "Title", "Concept"]
74-
data = [[f"Q{i}", x, f"Q{i}", "unknown"] for i, x in enumerate(answers, 1)]
76+
data = [[f"Q{i}", x, f"Q{i}", "unknown"] for i, x in enumerate(answers[:length], 1)]
7577
return [header] + data
7678

7779

0 commit comments

Comments
 (0)