32 lines
968 B
Python
32 lines
968 B
Python
|
"""Exemple d'utilisation des dumps."""
|
||
|
|
||
|
import json
|
||
|
from collections import Counter
|
||
|
|
||
|
|
||
|
def parse(filename):
|
||
|
with open(filename, "r") as file:
|
||
|
return json.load(file)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
Articles = {a["id"]: a for a in parse("article.dump.json")}
|
||
|
Operations = parse("operation.dump.json")
|
||
|
|
||
|
# Dépenses faites en K-Fêt depuis K-Psul
|
||
|
total_expenses = -sum((float(op["amount"]) for op in Operations))
|
||
|
print(f"total expenses: {total_expenses:.2f}€")
|
||
|
|
||
|
# Montant moyen d'une transaction
|
||
|
nb_ops = len(Operations)
|
||
|
print(f"Average transaction amount: {total_expenses / nb_ops:.2f}€")
|
||
|
|
||
|
# Nombre de ventes par article
|
||
|
articles_sales = Counter()
|
||
|
for operation in Operations:
|
||
|
articles_sales[operation["article"]] += operation["number"]
|
||
|
print("Most sold articles:")
|
||
|
for id, nb in articles_sales.most_common(3):
|
||
|
article_name = Articles[id]["name"]
|
||
|
print(f" - {article_name} : {nb}")
|