Python traps · Chapter 9 of 16
Generator exhaustion
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Time
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Python traps are free. This one comes with the all labs plan.
Why you need this
When an iterator runs out, next() raises StopIteration, and every loop treats that as "done", quietly. A loop over an exhausted generator simply runs zero times. sum of nothing is 0, and max with a default gives that default:
trip = pings()
total = sum(trip)
longest = max(trip, default=None)
print(total, longest)
8.0 None
Two safe shapes: walk once, working everything out in the same loop, or make a list once (kms = list(trip)) and walk that as often as you need. The list costs memory for the whole day; one pass costs none.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.py: right count, ₹0 revenue
Write reproduce() in repro.py. Build these three orders as a generator (a generator expression or a small generator function), the way read_orders hands orders to the report: {"id": "D2041", "customer": "C014", "amount": 170}, {"id": "D2042", "customer": "C022", "amount": 310} and {"id": "D2043", "customer": "C031", "amount": 140}. Pass it to daily_summary from report.py and return the dict it gives.
The check runs your reproduce() twice: beside the original report.py it must return {'orders': 3, 'revenue': 0, 'biggest': None} (the count is right and nothing is left after it), and beside a fixed report.py {'orders': 3, 'revenue': 620, 'biggest': 'D2042'}. A list instead of a generator does not show the bug. No real waiting and no input(): reproduce() must finish within 3 seconds, and you do not need a CSV file.
- Task 2
report.py: a report that reads its orders once
Fix daily_summary(orders) in report.py. Given any iterable of orders (a list, or a generator that can be walked only once), it returns {"orders": how many, "revenue": the sum of their "amount"s, "biggest": the "id" of the order with the largest amount (the first one, if two are equal)}. For no orders it returns {"orders": 0, "revenue": 0, "biggest": None}.
Keep read_orders (given, and still a generator) and report_line as they are. The check imports a fresh copy of your report.py and passes its own orders, as a list and as a one-shot iterator that notices being walked again after it ran out. Walking the orders once, or turning them into a list once at the start, are both fine.
- Task 3
test_report.py: a test that would have caught it
Write a pytest file, test_report.py, that would have caught this bug before it reached Rafiq. The check runs your file twice: beside the original report.py, where at least one test must fail, and beside the fixed report.py, where every test must pass.
So pass daily_summary its orders as a generator, the way read_orders does, and check the revenue and the biggest order, not only the count. A test that passes a list passes the bug too. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.py: right count, ₹0 revenue
- report.py: a report that reads its orders once
- test_report.py: a test that would have caught it
More in Time
Build "Generator exhaustion" yourself
DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python traps are free. This one comes with the all labs plan.