repytex/generate_bilan/generate_bilan.py

119 lines
3.7 KiB
Python
Raw Normal View History

2016-11-06 17:54:42 +00:00
#!/usr/bin/env python
# encoding: utf-8
from extract import extract_flat_marks, get_class_ws
from df_marks_manip import digest_flat_df, students_pov
from opytex import texenv
import pandas as pd
import numpy as np
import optparse
import xlrd
notStudent = ["Trimestre", "Nom", "Date", "Exercice", "Question", "Competence", "Domaine", "Commentaire", "Bareme", "Niveau"]
pure_marks = ["Malus", "Bonus", "Presentation"]
def extract_students(df, notStudent = notStudent):
""" Extract the list of students from df """
students = df.columns.difference(notStudent)
return students
def build_students(df1, df2, notStudent = notStudent):
""" Build students list """
students_from_notes = extract_students(df1, notStudent)
students_from_conn = extract_students(df2, notStudent)
if students_from_conn.equals(students_from_notes):
return students_from_conn
else:
raise ValueError("Not same list of students between df1 = {} ans df2 = {}".format(df1, df2))
def flat_df_students(df, students):
""" Flat the ws for students """
flat_df = pd.DataFrame()
flat_data = []
dfT = df.T
for n in dfT:
pre_di = dfT[n][notStudent].to_dict()
for e in students:
data = pre_di.copy()
data["Eleve"] = e
data["Note"] = dfT[n].loc[e]
flat_data.append(data)
return pd.DataFrame.from_dict(flat_data)
def select_ds(ds_name, flat_df):
"""TODO: Docstring for select_ds.
:param ds_name: TODO
:param flat_df: TODO
:returns: TODO
"""
ds = flat_df[flat_df["Nom"] == ds_name]
if len(ds) == 0:
raise ValueError("{} is not a registered evaluation".format(ds_name))
return ds
def build_ds_info(classe, ds_df):
"""TODO: Docstring for build_ds_info.
:param ds_df: TODO
:returns: TODO
# TODO: vérifier que toutes ces informations soient identiques sur les lignes |dim. nov. 6 16:06:58 EAT 2016
"""
ds_info = {}
ds_info["Classe"] = classe
ds_info["Nom"] = ds_df["Nom"].unique()[0]
ds_info["Date"] = pd.to_datetime(ds_df["Date"].unique()[0]).strftime("%d-%m-%Y")
ds_info["Trimestre"] = ds_df["Trimestre"].unique()[0]
return ds_info
def build_target_name(classe, ds_name):
return "./" + classe + "/bilan_" + ds_name + ".tex"
def feed_bilan(target, datas, template = "./tpl_bilan.tex"):
""" get the template and feed it to create bilans
:param ???:
:param datas: dictonnary to feed the template
:param template: the template
"""
bilan = texenv.get_template(template)
with open(target, "w") as f:
f.write(bilan.render(**datas))
print("{} est construit! Ya plus qu'à compiler!".format(target))
def build_bilan(classe, ds_name):
ws = get_class_ws(classe)
flat_df = extract_flat_marks(ws)
quest_df, exo_df, eval_df = digest_flat_df(flat_df)
quest_df = select_ds(ds_name, quest_df)
exo_df = select_ds(ds_name, exo_df)
eval_df = select_ds(ds_name, eval_df)
ds_info = build_ds_info(classe, eval_df)
students_df = students_pov(quest_df, exo_df, eval_df)
datas = {"ds_info": ds_info, "students":students_df}
target = build_target_name(classe, ds_name)
feed_bilan(target, datas)
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option("-c","--classe",action="store",type="string",dest="classe", help="The classe")
parser.add_option("-e","--evaluation",action="store",type="string",dest="ds_name", help="The evaluation name.")
(options, args) = parser.parse_args()
build_bilan(options.classe, options.ds_name)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del