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 os
import uuid
from pathlib import Path
import pandas as pd
from .. import Loader
@ -62,12 +63,15 @@ class CSVLoader(Loader):
"""Get exams list
: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:
>>> loader = CSVLoader("./test_config.yml")
>>> loader.get_exams(["Tribe1"])
Nom Date Trimestre score_file tribe
>>> exams = loader.get_exams(["Tribe1"])
>>> 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 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"])
exam = extract_fields(csv, fields)
exam = self.rename_columns(exam)
exam["score_file"] = csv
exam["origin"] = csv
exam["tribe"] = tribe
exam["id"] = uuid.uuid4()
exams.append(exam)
return pd.concat(exams)
@ -91,24 +96,31 @@ class CSVLoader(Loader):
"""
return ""
def get_exam_questions(self, score_files=[]):
def get_exam_questions(self, exams=[]):
"""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:
>>> loader = CSVLoader()
>>> loader.get_exam_questions(["example/Tribe1/210112_DS.csv"])
Exercice Question Competence ... Commentaire Bareme Est_nivele
0 Exercice 1 1 Calculer ... Coucou 1 1
1 Exercice 1 2 Calculer ... Coucou 1 1
2 Exercice 1 3 Calculer ... Coucou 1 1
3 Exercice 1 3 Calculer ... DChic 1 1
<BLANKLINE>
[4 rows x 7 columns]
>>> loader = CSVLoader("./test_config.yml")
>>> exams = loader.get_exams(["Tribe1"])
>>> loader.get_exam_questions([exams.iloc[0]]).columns
Index(['Exercice', 'Question', 'Competence', 'Domaine', 'Commentaire',
'Bareme', 'Est_nivele', 'origin', 'exam_id', 'id'],
dtype='object')
>>> questions = loader.get_exam_questions(exams)
>>> questions.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 = []
for score_file in score_files:
for exam in _exams:
fields = self.reverse_csv_field(
[
"exercise",
@ -120,8 +132,11 @@ class CSVLoader(Loader):
"is_leveled",
]
)
question = extract_fields(score_file, fields)
question = extract_fields(exam["origin"], fields)
question = self.rename_columns(question)
question["origin"] = exam["origin"]
question["exam_id"] = exam["id"]
question["id"] = uuid.uuid4()
questions.append(question)
return pd.concat(questions)