SALES_CSV = """\
date,product,region,quantity,unit_price
2024-01-05,Widget A,North,120,9.99
2024-01-05,Widget B,South,85,14.50
2024-01-12,Widget A,South,200,9.99
...
"""
ANALYSIS_SCRIPT = """\
import csv, json
from collections import defaultdict
rows = []
with open("/home/user/data/sales.csv") as f:
reader = csv.DictReader(f)
for row in reader:
row["quantity"] = int(row["quantity"])
row["unit_price"] = float(row["unit_price"])
row["revenue"] = row["quantity"] * row["unit_price"]
rows.append(row)
total_revenue = sum(r["revenue"] for r in rows)
rev_by_product = defaultdict(float)
for r in rows:
rev_by_product[r["product"]] += r["revenue"]
top_product = max(rev_by_product, key=rev_by_product.get)
results = {
"total_records": len(rows),
"total_revenue": round(total_revenue, 2),
"top_product": top_product,
"revenue_by_product": {k: round(v, 2) for k, v in sorted(rev_by_product.items())},
}
with open("/home/user/data/results.json", "w") as f:
json.dump(results, f, indent=2)
print("Analysis complete. Results written to /home/user/data/results.json")
"""