SPB Git

Repository Public MIT Research preview

rapport.py 3.1 KB 81 lines python

1"""Rapport des ventes — candidat A, rédigé pour le cours ATLAS IA (S14). Ce n'est pas la sortie d'un outil.23Remaniement proposé : la longue fonction est découpée, le calcul des totaux n'est plus recopié.4Interface publique inchangée : produire(chemin, fmt), avec fmt valant "texte" ou "csv".5"""6import os78TAUX = 0.159TOTAUX = {}10ARTICLES = {}11NB = 012GABARITS = {"texte": "%-14s %8d %11.2f %9.2f %11.2f\n", "csv": "%s,%d,%.2f,%.2f,%.2f\n"}131415def analyser(numero, ligne):16    champs = ligne.split(",")17    if len(champs) != 6:18        raise ValueError("ligne %d : 6 champs attendus, %d trouvés" % (numero, len(champs)))19    try:20        quantite, prix, rabais = int(champs[3]), float(champs[4]), float(champs[5])21    except ValueError:22        raise ValueError("ligne %d : nombre invalide" % numero)23    if quantite < 0 or prix < 0 or rabais < 0 or rabais > 100:24        raise ValueError("ligne %d : valeur hors limites" % numero)25    return champs[1].strip().lower(), champs[2].strip().lower(), quantite, quantite * prix * (1 - rabais / 100)262728def lire(chemin):29    global NB30    with open(chemin, encoding="utf-8") as fichier:31        lignes = fichier.read().split("\n")32    for numero, ligne in enumerate(lignes, start=1):33        if numero == 1 or ligne.strip() == "":34            continue35        categorie, article, quantite, montant = analyser(numero, ligne)36        if categorie not in TOTAUX:37            TOTAUX[categorie] = [0, 0.0]38        TOTAUX[categorie][0] += quantite39        TOTAUX[categorie][1] += montant40        ARTICLES[article] = ARTICLES.get(article, 0) + quantite41        NB += 1424344def resumer():45    lignes, quantite_totale, sous_total_general, taxes_generales = [], 0, 0.0, 0.046    for categorie in sorted(TOTAUX):47        sous_total = round(TOTAUX[categorie][1], 2)48        taxes = round(sous_total * TAUX, 2)49        quantite_totale += TOTAUX[categorie][0]50        sous_total_general += sous_total51        taxes_generales += taxes52        lignes.append((categorie, TOTAUX[categorie][0], sous_total, taxes, sous_total + taxes))53    lignes.append(("TOTAL", quantite_totale, sous_total_general, taxes_generales,54                   sous_total_general + taxes_generales))55    return lignes565758def rapport_texte(chemin):59    texte = "Coopérative Les Semis - rapport des ventes\n"60    texte += "Fichier : " + os.path.basename(chemin) + "\n"61    texte += "Lignes lues : " + str(NB) + "\n\n"62    if NB == 0:63        return texte + "Aucune vente.\n"64    texte += "%-14s %8s %11s %9s %11s\n" % ("Catégorie", "Quantité", "Sous-total", "Taxes", "Total")65    texte += "".join(GABARITS["texte"] % ligne for ligne in resumer())66    article, quantite = min(ARTICLES.items(), key=lambda paire: (-paire[1], paire[0]))67    return texte + "\nArticle le plus vendu : " + article + " (" + str(quantite) + ")\n"686970def rapport_csv():71    return "categorie,quantite,sous_total,taxes,total\n" + "".join(GABARITS["csv"] % ligne for ligne in resumer())727374def produire(chemin, fmt):75    lire(chemin)76    if fmt == "texte":77        return rapport_texte(chemin)78    if fmt == "csv":79        return rapport_csv()80    raise ValueError("format inconnu : " + str(fmt))81
Files main
Menu

Theme