SPB Git

Repository Public MIT Research preview

rapport.py 3.3 KB 80 lines python

1"""Rapport des ventes — candidat B, 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, les variables de module ont disparu et le calcul4est « simplifié » : chaque vente porte son montant et sa taxe, qu'il suffit d'additionner.5Interface publique inchangée : produire(chemin, fmt), avec fmt valant "texte" ou "csv".6"""7import os89TAUX = 0.1510GABARITS = {"texte": "%-14s %8d %11.2f %9.2f %11.2f\n", "csv": "%s,%d,%.2f,%.2f,%.2f\n"}111213def analyser(numero, ligne):14    champs = ligne.split(",")15    if len(champs) != 6:16        raise ValueError("ligne %d : 6 champs attendus, %d trouvés" % (numero, len(champs)))17    try:18        quantite, prix, rabais = int(champs[3]), float(champs[4]), float(champs[5])19    except ValueError:20        raise ValueError("ligne %d : nombre invalide" % numero)21    if quantite < 0 or prix < 0 or rabais < 0 or rabais > 100:22        raise ValueError("ligne %d : valeur hors limites" % numero)23    montant = quantite * prix * (1 - rabais / 100)24    return champs[1].strip().lower(), champs[2].strip().lower(), quantite, montant, round(montant * TAUX, 2)252627def lire(chemin):28    with open(chemin, encoding="utf-8") as fichier:29        lignes = fichier.read().split("\n")30    return [analyser(numero, ligne) for numero, ligne in enumerate(lignes, start=1)31            if numero > 1 and ligne.strip() != ""]323334def resumer(ventes):35    lignes, quantite_totale, sous_total_general, taxes_generales = [], 0, 0.0, 0.036    for categorie in sorted({vente[0] for vente in ventes}):37        du_rayon = [vente for vente in ventes if vente[0] == categorie]38        quantite = sum(vente[2] for vente in du_rayon)39        sous_total = round(sum(vente[3] for vente in du_rayon), 2)40        taxes = round(sum(vente[4] for vente in du_rayon), 2)41        quantite_totale += quantite42        sous_total_general += sous_total43        taxes_generales += taxes44        lignes.append((categorie, quantite, sous_total, taxes, sous_total + taxes))45    lignes.append(("TOTAL", quantite_totale, sous_total_general, taxes_generales,46                   sous_total_general + taxes_generales))47    return lignes484950def article_le_plus_vendu(ventes):51    quantites = {}52    for vente in ventes:53        quantites[vente[1]] = quantites.get(vente[1], 0) + vente[2]54    return min(quantites.items(), key=lambda paire: (-paire[1], paire[0]))555657def rapport_texte(chemin, ventes):58    texte = "Coopérative Les Semis - rapport des ventes\n"59    texte += "Fichier : " + os.path.basename(chemin) + "\n"60    texte += "Lignes lues : " + str(len(ventes)) + "\n\n"61    if not ventes:62        return texte + "Aucune vente.\n"63    texte += "%-14s %8s %11s %9s %11s\n" % ("Catégorie", "Quantité", "Sous-total", "Taxes", "Total")64    texte += "".join(GABARITS["texte"] % ligne for ligne in resumer(ventes))65    article, quantite = article_le_plus_vendu(ventes)66    return texte + "\nArticle le plus vendu : " + article + " (" + str(quantite) + ")\n"676869def rapport_csv(ventes):70    return "categorie,quantite,sous_total,taxes,total\n" + "".join(GABARITS["csv"] % ligne for ligne in resumer(ventes))717273def produire(chemin, fmt):74    ventes = lire(chemin)75    if fmt == "texte":76        return rapport_texte(chemin, ventes)77    if fmt == "csv":78        return rapport_csv(ventes)79    raise ValueError("format inconnu : " + str(fmt))80
Files main
Menu

Theme