Feat: add id to exam and questions

This commit is contained in:
Bertrand Benjamin 2021-04-06 18:57:41 +02:00
parent 37c95d75de
commit 596a52b1d0

View File

@ -3,6 +3,7 @@
import yaml import yaml
import os import os
import uuid
from pathlib import Path from pathlib import Path
import pandas as pd import pandas as pd
from .. import Loader from .. import Loader
@ -62,12 +63,15 @@ class CSVLoader(Loader):
"""Get exams list """Get exams list
:param tribes: get only exams for those tribes :param tribes: get only exams for those tribes
:return: list of dictionaries of exams (fields: `["exam", "tribe", "date", "term", "score_file"]`) :return: dataframe of exams
:example: :example:
>>> loader = CSVLoader("./test_config.yml") >>> loader = CSVLoader("./test_config.yml")
>>> loader.get_exams(["Tribe1"]) >>> exams = loader.get_exams(["Tribe1"])
Nom Date Trimestre score_file tribe >>> exams.columns
Index(['Nom', 'Date', 'Trimestre', 'origin', 'tribe', 'id'], dtype='object')
>>> exams.loc[:, exams.columns!="id"]
Nom Date Trimestre origin tribe
0 DS 12/01/2021 1 example/Tribe1/210112_DS.csv Tribe1 0 DS 12/01/2021 1 example/Tribe1/210112_DS.csv Tribe1
0 DS6 22/01/2021 1 example/Tribe1/210122_DS6.csv Tribe1 0 DS6 22/01/2021 1 example/Tribe1/210122_DS6.csv Tribe1
""" """
@ -79,8 +83,9 @@ class CSVLoader(Loader):
fields = self.reverse_csv_field(["exam", "date", "term"]) fields = self.reverse_csv_field(["exam", "date", "term"])
exam = extract_fields(csv, fields) exam = extract_fields(csv, fields)
exam = self.rename_columns(exam) exam = self.rename_columns(exam)
exam["score_file"] = csv exam["origin"] = csv
exam["tribe"] = tribe exam["tribe"] = tribe
exam["id"] = uuid.uuid4()
exams.append(exam) exams.append(exam)
return pd.concat(exams) return pd.concat(exams)
@ -91,24 +96,31 @@ class CSVLoader(Loader):
""" """
return "" return ""
def get_exam_questions(self, score_files=[]): def get_exam_questions(self, exams=[]):
"""Get questions for exams stored in score_files """Get questions for exams stored in score_files
:param exams: questions for those exams only :param exams: list of exams metadatas (need origin field to find the csv)
:example: :example:
>>> loader = CSVLoader() >>> loader = CSVLoader("./test_config.yml")
>>> loader.get_exam_questions(["example/Tribe1/210112_DS.csv"]) >>> exams = loader.get_exams(["Tribe1"])
Exercice Question Competence ... Commentaire Bareme Est_nivele >>> loader.get_exam_questions([exams.iloc[0]]).columns
0 Exercice 1 1 Calculer ... Coucou 1 1 Index(['Exercice', 'Question', 'Competence', 'Domaine', 'Commentaire',
1 Exercice 1 2 Calculer ... Coucou 1 1 'Bareme', 'Est_nivele', 'origin', 'exam_id', 'id'],
2 Exercice 1 3 Calculer ... Coucou 1 1 dtype='object')
3 Exercice 1 3 Calculer ... DChic 1 1 >>> questions = loader.get_exam_questions(exams)
<BLANKLINE> >>> questions.columns
[4 rows x 7 columns] Index(['Exercice', 'Question', 'Competence', 'Domaine', 'Commentaire',
'Bareme', 'Est_nivele', 'origin', 'exam_id', 'id'],
dtype='object')
""" """
try:
_exams = [e[1] for e in exams.iterrows()]
except AttributeError:
_exams = exams
questions = [] questions = []
for score_file in score_files: for exam in _exams:
fields = self.reverse_csv_field( fields = self.reverse_csv_field(
[ [
"exercise", "exercise",
@ -120,8 +132,11 @@ class CSVLoader(Loader):
"is_leveled", "is_leveled",
] ]
) )
question = extract_fields(score_file, fields) question = extract_fields(exam["origin"], fields)
question = self.rename_columns(question) question = self.rename_columns(question)
question["origin"] = exam["origin"]
question["exam_id"] = exam["id"]
question["id"] = uuid.uuid4()
questions.append(question) questions.append(question)
return pd.concat(questions) return pd.concat(questions)