From 3e98d938087d2af32511255bb858ea264a3c6ca5 Mon Sep 17 00:00:00 2001 From: karecal <155586481+karecal@users.noreply.github.com> Date: Thu, 21 May 2026 19:10:29 +0100 Subject: [PATCH] done --- lab-python-d1/analisis.py | 79 ++++++++++++++++++++++++++++++++++++++ lab-python-d1/informe.json | 11 ++++++ lab-python-d1/ventas.json | 9 +++++ 3 files changed, 99 insertions(+) create mode 100644 lab-python-d1/analisis.py create mode 100644 lab-python-d1/informe.json create mode 100644 lab-python-d1/ventas.json diff --git a/lab-python-d1/analisis.py b/lab-python-d1/analisis.py new file mode 100644 index 0000000..98816f5 --- /dev/null +++ b/lab-python-d1/analisis.py @@ -0,0 +1,79 @@ +import json +from datetime import datetime + +def cargar_ventas(ruta_archivo): + """Lee el archivo JSON y devuelve la lista de ventas.""" + with open(ruta_archivo, "r", encoding="utf-8") as f: + return json.load(f) + +def calcular_total_venta(venta): + """Devuelve precio * cantidad para una venta.""" + return venta["precio"] * venta["cantidad"] + +def ventas_por_categoria(ventas): + """ + Agrupa las ventas por categoría. + Devuelve un dict: { "categoria": total_euros } + """ + categorias = {} + for venta in ventas: + cat = venta["categoria"] + total = calcular_total_venta(venta) + # Si la categoría ya existe, suma; si no, la inicializa + categorias[cat] = categorias.get(cat, 0) + total + return categorias + +def producto_mas_vendido(ventas): + """Devuelve el nombre del producto con mayor ingreso total.""" + mejor = max(ventas, key=calcular_total_venta) + return mejor["producto"], calcular_total_venta(mejor) + +def ventas_en_fecha(ventas, fecha_str): + """Filtra ventas de una fecha específica (formato YYYY-MM-DD).""" + return [v for v in ventas if v["fecha"] == fecha_str] + +def guardar_informe(informe, ruta): + """Guarda el informe como JSON en la ruta indicada.""" + with open(ruta, "w", encoding="utf-8") as f: + json.dump(informe, f, ensure_ascii=False, indent=2) + +def main(): + ventas = cargar_ventas("ventas.json") + + total_ventas = len(ventas) + ingresos_totales = sum(calcular_total_venta(v) for v in ventas) + categorias = ventas_por_categoria(ventas) + producto_top, ingreso_top = producto_mas_vendido(ventas) + ventas_16 = ventas_en_fecha(ventas, "2026-01-16") + + # --- Imprimir informe --- + print("============================") + print(" INFORME DE VENTAS") + print("============================\n") + print(f"Total de ventas: {total_ventas}") + print(f"Ingresos totales: {ingresos_totales:,.2f} €\n".replace(",", "X").replace(".", ",").replace("X", ".")) + + print("--- Por categoría ---") + for cat, total in categorias.items(): + print(f"{cat}: {total:,.2f} €".replace(",", "X").replace(".", ",").replace("X", ".")) + + print(f"\nProducto más rentable: {producto_top} ({ingreso_top:,.2f} €)".replace(",", "X").replace(".", ",").replace("X", ".")) + + print("\n--- Ventas del 16/01/2026 ---") + for v in ventas_16: + total = calcular_total_venta(v) + print(f"- {v['producto']}: {total:,.2f} €".replace(",", "X").replace(".", ",").replace("X", ".")) + + # --- Paso 4: Exportar informe JSON --- + informe = { + "generado_en": datetime.now().strftime("%Y-%m-%dT%H:%M:%S"), + "total_ventas": total_ventas, + "ingresos_totales": round(ingresos_totales, 2), + "por_categoria": {cat: round(total, 2) for cat, total in categorias.items()}, + "producto_top": producto_top + } + guardar_informe(informe, "informe.json") + print("\n✓ Informe guardado en informe.json") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/lab-python-d1/informe.json b/lab-python-d1/informe.json new file mode 100644 index 0000000..ee2885f --- /dev/null +++ b/lab-python-d1/informe.json @@ -0,0 +1,11 @@ +{ + "generado_en": "2026-05-21T19:09:00", + "total_ventas": 7, + "ingresos_totales": 4519.74, + "por_categoria": { + "electronica": 3419.86, + "muebles": 799.98, + "libros": 299.9 + }, + "producto_top": "Laptop" +} \ No newline at end of file diff --git a/lab-python-d1/ventas.json b/lab-python-d1/ventas.json new file mode 100644 index 0000000..6c5022e --- /dev/null +++ b/lab-python-d1/ventas.json @@ -0,0 +1,9 @@ +[ + { "id": 1, "producto": "Laptop", "precio": 899.99, "cantidad": 2, "categoria": "electronica", "fecha": "2026-01-15" }, + { "id": 2, "producto": "Teclado", "precio": 49.99, "cantidad": 5, "categoria": "electronica", "fecha": "2026-01-16" }, + { "id": 3, "producto": "Silla", "precio": 299.99, "cantidad": 1, "categoria": "muebles", "fecha": "2026-01-16" }, + { "id": 4, "producto": "Monitor", "precio": 349.99, "cantidad": 3, "categoria": "electronica", "fecha": "2026-01-17" }, + { "id": 5, "producto": "Libro Python", "precio": 29.99, "cantidad": 10, "categoria": "libros", "fecha": "2026-01-18" }, + { "id": 6, "producto": "Escritorio", "precio": 499.99, "cantidad": 1, "categoria": "muebles", "fecha": "2026-01-18" }, + { "id": 7, "producto": "Auriculares", "precio": 79.99, "cantidad": 4, "categoria": "electronica", "fecha": "2026-01-19" } +] \ No newline at end of file