added helper for JACK3
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
import argparse
|
||||
import re
|
||||
from os import listdir
|
||||
from os.path import isfile, join
|
||||
|
||||
|
||||
TESTAT_COUNT = 3
|
||||
|
||||
|
||||
def get_testat_points(input_dir):
|
||||
input_files = [f for f in listdir(input_dir) if isfile(join(input_dir, f)) and f.endswith('.csv')]
|
||||
|
||||
# key: student id, value: results for each testat, or -1 if not participated
|
||||
results: dict[str, list[int]] = {}
|
||||
|
||||
# key: student id, value (student id, first name, last name, mat number, participations)
|
||||
student_info: dict[str, tuple[str, str, str, str, list[str]]] = {}
|
||||
|
||||
for input_file in input_files:
|
||||
with open(join(input_dir, input_file)) as file:
|
||||
testat_results = [line.strip() for line in file][1:]
|
||||
for r in testat_results:
|
||||
split = r.split(",")
|
||||
student_id = split[0][1:-1]
|
||||
if student_id not in results.keys():
|
||||
results[student_id] = [-1 for _ in range(TESTAT_COUNT)]
|
||||
if student_id not in student_info.keys():
|
||||
student_info[student_id] = (student_id, split[3][1:-1], split[2][1:-1], split[4][1:-1],
|
||||
["" for _ in range(TESTAT_COUNT)])
|
||||
try:
|
||||
testat_number = int(input_file.split(" ")[1])
|
||||
if testat_number > TESTAT_COUNT:
|
||||
raise ValueError
|
||||
testat_index = testat_number - 1
|
||||
if len(student_info[student_id][4][testat_index]) != 0:
|
||||
raise Exception
|
||||
student_info[student_id][4][testat_index] = " ".join(input_file.split(" ")[2:5])
|
||||
if testat_index >= TESTAT_COUNT:
|
||||
raise ValueError("testat index out of range")
|
||||
except ValueError: # result of Ersatztestat
|
||||
testat_number = input_file.split(" ")[0]
|
||||
testat_index = TESTAT_COUNT
|
||||
results[student_id].append(-1)
|
||||
score = split[-1]
|
||||
score = int(re.sub('[^0-9]', '', score))
|
||||
if score not in range(0, 101):
|
||||
raise ValueError(f"invalid score: {str(score)}")
|
||||
if testat_index not in range(0, len(results[student_id])):
|
||||
raise ValueError(f"invalid testat: {str(testat_number)}")
|
||||
if results[student_id][testat_index] != -1:
|
||||
raise ValueError(f"found duplicate entry in testat {str(testat_number)}")
|
||||
results[student_id][testat_index] = score
|
||||
|
||||
return results, student_info
|
||||
|
||||
|
||||
def filter_students(input_dir, registrations, output):
|
||||
results, info = get_testat_points(input_dir)
|
||||
if len([l for l in results.values() if len(l) > TESTAT_COUNT]) > 0:
|
||||
raise ValueError("student participated in too many testats")
|
||||
with open(registrations) as file:
|
||||
registered_students = set([line.strip() for line in file])
|
||||
filtered_students = set()
|
||||
for student in registered_students:
|
||||
if student not in results.keys():
|
||||
results[student] = [-1 for _ in range(TESTAT_COUNT)]
|
||||
missed = [i for i, s in enumerate(results[student]) if s == -1]
|
||||
if len(missed) == 0:
|
||||
print(f"student {student} ({info[student][1]} {info[student][2]}) "
|
||||
f"participated in all testats: {info[student][4]}")
|
||||
else:
|
||||
filtered_students.add(student)
|
||||
with open(output, "w") as file:
|
||||
for student in filtered_students:
|
||||
file.write(f"{student}\n")
|
||||
|
||||
|
||||
def merge_results(input_dir, output):
|
||||
results, student_info = get_testat_points(input_dir)
|
||||
with open(output, "w") as file:
|
||||
testate = [f"testat_{(i+1)}" for i in range(TESTAT_COUNT)]
|
||||
testate.append("ersatztestat")
|
||||
file.write(f"student_id;first_name;last_name;mat_number;{";".join(testate)};sum;Zulassung\n")
|
||||
for student_id, result in results.items():
|
||||
line = (f"{student_id};{student_info[student_id][1]};"
|
||||
f"{student_info[student_id][2]};{student_info[student_id][3]};")
|
||||
if len(result) == TESTAT_COUNT:
|
||||
result.append(-1)
|
||||
res_str = [str(r) if r != -1 else "nicht anwesend" for r in result]
|
||||
res_score = [r if r != -1 else 0 for r in result]
|
||||
line = line + ";".join(res_str)
|
||||
line = line + ";" + str(sum(res_score))
|
||||
line = line + ";" + ("Zulassung" if sum(res_score) >= TESTAT_COUNT * 50 else "Keine Zulassung")
|
||||
file.write(line + "\n")
|
||||
|
||||
|
||||
|
||||
def start(input_dir: str, registrations: str, output: str, zusatz: bool):
|
||||
if zusatz:
|
||||
filter_students(input_dir, registrations, output)
|
||||
else:
|
||||
merge_results(input_dir, output)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-d", "--input_dir", type=str, required=True,
|
||||
help='path to a directory with results from jack', action='store')
|
||||
parser.add_argument("-r", "--registrations", type=str,
|
||||
help='path to a file with people registered for the zusatztestat, only valid with -z')
|
||||
parser.add_argument("-o", "--output", type=str, required=True, help='the output file', action='store')
|
||||
parser.add_argument("-z", "--zusatz", action='store_true',
|
||||
help='set this if you are setting up the Zusatztestat')
|
||||
args = parser.parse_args()
|
||||
start(args.input_dir, args.registrations, args.output, args.zusatz)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user