Twenty lessons. Today: stitch five into a reliable chain on a 3-element input. No new APIs, no new patterns.
Given items = ["a", "b", "c"] and a forced-failure profile:
"a" succeeds first try"b" fails once, succeeds on retry"c" fails on every retry (permanent)Expected behavior:
seen set (would be a Sheet in production)"c" fails without crashing the whole script"c"), print one alert lineimport json, time
def log(event, **fields):
print(json.dumps({"event": event, **fields}))
attempts = {"a": 0, "b": 0, "c": 0}
def process(item):
attempts[item] += 1
if item == "b" and attempts[item] < 2:
raise RuntimeError("b transient")
if item == "c":
raise RuntimeError("c permanent")
return f"ok-{item}"
seen = set()
results = {"ok": 0, "fail": 0}
alerts = []
for item in ["a", "b", "c"]:
if item in seen:
log("item", id=item, status="skipped")
continue
last_error = None
success = False
for attempt in range(1, 3): # 2 attempts
try:
process(item)
success = True
break
except Exception as e:
last_error = e
time.sleep(0) # placeholder for backoff
if success:
seen.add(item)
results["ok"] += 1
status = "ok-after-retry" if attempts[item] > 1 else "ok"
log("item", id=item, status=status)
else:
results["fail"] += 1
alerts.append(item)
log("item", id=item, status="failed", error=type(last_error).__name__)
log("summary", **results)
if alerts:
log("alert", failed=alerts)Five primitives composed. The script doesn't crash; c triggers an alert; a and b are processed once.
Five concepts on three items. None new?
Zero new. Retry (day 9), dedup (day 11), partial failure (day 13), structured logging (day 14), side-effect-on-condition (week 1). The skill: assemble them into one tight script without overthinking.
| Primitive | Lesson | Used for |
|---|---|---|
| Retry with backoff | Day 9 | wrapping each item's process call in 2 attempts |
| Dedup | Day 11 | seen = set(), skip if already processed |
| Partial failure | Day 13 | c fails without crashing the script |
| Structured logging | Day 14 | log("item", id=..., status=...) per outcome |
| Side effect | Week 1 | the alerts list / log("alert", ...) line |
No new concepts. The exam is recognition — given a script shape, pick the right primitives.
Deliberately not in scope:
seen, not a Sheet — that's day 12; combining with retry was deliberately split)process() with a controllable failure profile so the lesson is deterministic)A real production script combines all of these. Today: the reliability layer in isolation.
The outer for-loop owns dedup. The inner for-loop owns retry. The try/except sits inside the inner loop. The success/failure decision happens at the inner loop's exit.
for item in items: # outer: dedup
if item in seen: continue
for attempt in range(1, 3): # inner: retry
try:
process(item)
success = True
break # success exits inner loop
except Exception as e:
last_error = e
# decide on outcome of inner loop
if success:
seen.add(item)
log(...)
else:
alerts.append(item)
log(...)Four clean nesting levels. Once you see this shape, you'll write it for any reliable-chain.
We use a Python process() function with a deterministic failure pattern so the verification can assert exact counts. Real Gmail/Calendar would be flaky on different items in different runs — fine for production, terrible for grading. The pattern transfers 1:1 to real tool calls.
Twenty lessons. Today: stitch five into a reliable chain on a 3-element input. No new APIs, no new patterns.
Given items = ["a", "b", "c"] and a forced-failure profile:
"a" succeeds first try"b" fails once, succeeds on retry"c" fails on every retry (permanent)Expected behavior:
seen set (would be a Sheet in production)"c" fails without crashing the whole script"c"), print one alert lineimport json, time
def log(event, **fields):
print(json.dumps({"event": event, **fields}))
attempts = {"a": 0, "b": 0, "c": 0}
def process(item):
attempts[item] += 1
if item == "b" and attempts[item] < 2:
raise RuntimeError("b transient")
if item == "c":
raise RuntimeError("c permanent")
return f"ok-{item}"
seen = set()
results = {"ok": 0, "fail": 0}
alerts = []
for item in ["a", "b", "c"]:
if item in seen:
log("item", id=item, status="skipped")
continue
last_error = None
success = False
for attempt in range(1, 3): # 2 attempts
try:
process(item)
success = True
break
except Exception as e:
last_error = e
time.sleep(0) # placeholder for backoff
if success:
seen.add(item)
results["ok"] += 1
status = "ok-after-retry" if attempts[item] > 1 else "ok"
log("item", id=item, status=status)
else:
results["fail"] += 1
alerts.append(item)
log("item", id=item, status="failed", error=type(last_error).__name__)
log("summary", **results)
if alerts:
log("alert", failed=alerts)Five primitives composed. The script doesn't crash; c triggers an alert; a and b are processed once.
Five concepts on three items. None new?
Zero new. Retry (day 9), dedup (day 11), partial failure (day 13), structured logging (day 14), side-effect-on-condition (week 1). The skill: assemble them into one tight script without overthinking.
| Primitive | Lesson | Used for |
|---|---|---|
| Retry with backoff | Day 9 | wrapping each item's process call in 2 attempts |
| Dedup | Day 11 | seen = set(), skip if already processed |
| Partial failure | Day 13 | c fails without crashing the script |
| Structured logging | Day 14 | log("item", id=..., status=...) per outcome |
| Side effect | Week 1 | the alerts list / log("alert", ...) line |
No new concepts. The exam is recognition — given a script shape, pick the right primitives.
Deliberately not in scope:
seen, not a Sheet — that's day 12; combining with retry was deliberately split)process() with a controllable failure profile so the lesson is deterministic)A real production script combines all of these. Today: the reliability layer in isolation.
The outer for-loop owns dedup. The inner for-loop owns retry. The try/except sits inside the inner loop. The success/failure decision happens at the inner loop's exit.
for item in items: # outer: dedup
if item in seen: continue
for attempt in range(1, 3): # inner: retry
try:
process(item)
success = True
break # success exits inner loop
except Exception as e:
last_error = e
# decide on outcome of inner loop
if success:
seen.add(item)
log(...)
else:
alerts.append(item)
log(...)Four clean nesting levels. Once you see this shape, you'll write it for any reliable-chain.
We use a Python process() function with a deterministic failure pattern so the verification can assert exact counts. Real Gmail/Calendar would be flaky on different items in different runs — fine for production, terrible for grading. The pattern transfers 1:1 to real tool calls.
Create a free account to get started. Paid plans unlock all tracks.