Case Studies
Expense Summary
A small report can filter records and then group totals by category.
Expense Summary
expense_summary.py
expenses = [
{"category": "tools", "amount": 18},
{"category": "books", "amount": 35},
{"category": "tools", "amount": 12},
]
min_amount =
totals = {}
for expense in expenses:
if expense["amount"] < min_amount:
continue
category = expense["category"]
totals[category] = totals.get(category, 0) + expense["amount"]
parts = []
for category in sorted(totals):
parts.append(category + "=" + str(totals[category]))
print("summary=" + ";".join(parts))
expenses = [
{"category": "tools", "amount": 18},
{"category": "books", "amount": 35},
{"category": "tools", "amount": 12},
]
min_amount =
totals = {}
for expense in expenses:
if expense["amount"] < min_amount:
continue
category = expense["category"]
totals[category] = totals.get(category, 0) + expense["amount"]
parts = []
for category in sorted(totals):
parts.append(category + "=" + str(totals[category]))
print("summary=" + ";".join(parts))
report pipeline
Case-study code often combines earlier ideas: filtering, grouping, and sorted output.