From 9e0ea14d05fd1e20c73cc5a7826c3ad2882348a1 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 18 Apr 2021 17:20:54 +0200 Subject: [PATCH] Feat: remove relative imports and simplify get_unstack_scores --- .../dashboard/pages/exams_scores/callbacks.py | 2 +- recopytex/dashboard/pages/exams_scores/models.py | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/recopytex/dashboard/pages/exams_scores/callbacks.py b/recopytex/dashboard/pages/exams_scores/callbacks.py index 4628d8e..b6ed1de 100644 --- a/recopytex/dashboard/pages/exams_scores/callbacks.py +++ b/recopytex/dashboard/pages/exams_scores/callbacks.py @@ -47,7 +47,7 @@ def update_scores_store(exam): if not exam: return [[], [], [], {}] exam = pd.DataFrame.from_dict([json.loads(exam)]) - scores = get_unstack_scores(exam, "score", "student_name") + scores = get_unstack_scores(exam) fixed_columns = [ "exercise", "question", diff --git a/recopytex/dashboard/pages/exams_scores/models.py b/recopytex/dashboard/pages/exams_scores/models.py index bc1638c..4d7df5c 100644 --- a/recopytex/dashboard/pages/exams_scores/models.py +++ b/recopytex/dashboard/pages/exams_scores/models.py @@ -1,7 +1,8 @@ #!/usr/bin/env python # encoding: utf-8 -from ....database.filesystem.loader import CSVLoader +from recopytex.database.filesystem.loader import CSVLoader +from recopytex.lib.dataframe import column_values_to_column LOADER = CSVLoader("./test_config.yml") @@ -18,15 +19,10 @@ def get_record_scores(exam): return LOADER.get_exam_scores(exam) -def get_unstack_scores(exam, value_column="score", unstack_column="student_name"): +def get_unstack_scores(exam): flat_scores = LOADER.get_exam_scores(exam) - pivot_columns = [col for col in LOADER.score_columns if col != value_column] - unstacked_df = ( - flat_scores.set_index(pivot_columns)[value_column] - .unstack(unstack_column) - .reset_index() - ) - return unstacked_df + kept_columns = [col for col in LOADER.score_columns if col != "score"] + return column_values_to_column(flat_scores, "student_name", "score", kept_columns) def get_students_from_exam(exam):