Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
Why should an automated job avoid sending every item at once?
Show the answer
Smaller batches limit failure impact, memory use, and service pressure.
Bound the work and expose retry exhaustion
batches is a generator: calling it creates an iterator; each iteration reaches yield and supplies the next slice. range(0, len(items), size) advances by the batch size, and items[start:start + size] keeps the last partial batch. Empty input yields no batches. Use a positive non-boolean integer size; the exercises reject invalid sizes with ValueError when consumed.
The mock fetch raises RuntimeError twice and then succeeds. A three-attempt loop must break after success, print Retry only when another attempt remains, and re-raise the last failure on exhaustion. Catch this declared mock error, not every exception: unrelated ValueError must remain visible. An exhausted loop is not a successful empty result.
This exercise makes no network calls and waits zero real seconds. Real retries need a service-specific transient-error policy, capped backoff and a check that repeating the operation is safe; do not blindly retry every RuntimeError or repeat a non-idempotent write. The complete helper receives a callable, allowing immediate success and exhaustion to be tested without a network.
# Re-raise the last mock failure rather than hiding it.
def retry(fetch, attempts=3):
for attempt in range(1, attempts + 1):
try:
return fetch()
except RuntimeError:
if attempt == attempts:
raise
def unavailable():
raise RuntimeError("still unavailable")
print(retry(lambda: "ready"))
try:
retry(unavailable)
except RuntimeError as error:
print("Exhausted:", error)
Immediate success returns ready; three failures remain a visible error. No fourth call is made.
The helper assumes attempts is a positive integer. Validate configurable limits in a production boundary; the coding task uses exactly three.
Predict, explain, then test
These three comparisons are self-reviewed, not scored. Coding checks run new inputs; the section quiz records separate knowledge evidence.
Trace a partial batch
Five items, size three: how many batches and what sizes?
Compare your answer · self-reviewed
Two batches, of three and two; do not drop the final partial batch.
Bound a retry
All three declared mock attempts fail. What must happen?
Compare your answer · self-reviewed
Re-raise the last RuntimeError; do not make a fourth call or silently return success.
Limit the policy
Is every real RuntimeError safe to retry?
Compare your answer · self-reviewed
No. This is a declared mock error. Real services need transient-error classification and safe repeat semantics.
Choose an exercise to load its prompt.
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.