Last code lesson of the track. We tie five week-4 patterns into one short script:
print per kept email — standing in for an action like send_slack per email (L25).import time
def fetch_emails_paginated(pages=2, per_page=5):
all_msgs, page_token = [], None
for _ in range(pages):
args = {"max_results": per_page}
if page_token:
args["page_token"] = page_token
for attempt in range(1, 4):
try:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, args)
break
except Exception:
time.sleep(2 ** attempt)
else:
raise RuntimeError("all retries failed")
all_msgs.extend(result.get("messages", []))
page_token = result.get("nextPageToken")
if not page_token:
break
return all_msgs
emails = fetch_emails_paginated(pages=2, per_page=5)
filtered = [e for e in emails if e.get("subject")]
for e in filtered:
print(f"would notify: {e.get('subject')}")
print(f"total notified: {len(filtered)}")Every line uses a primitive I learned earlier in the track.
That's the point. No new tools, no new concepts — just composition. This is what "writing automation" looks like in practice: not memorising one big script, but knowing the small patterns well enough to compose them on demand.
No new tool. No new concept. Composition of week-4 + earlier patterns into one shape.
def fetch_emails_paginated(pages=2, per_page=5):→ Helper (L24). Named, reusable, defaults on args.
all_msgs, page_token = [], None
for _ in range(pages):
args = {"max_results": per_page}
if page_token:
args["page_token"] = page_token→ Pagination scaffold (L23). Outer loop bounded by pages; per-iteration args dict.
for attempt in range(1, 4):
try:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, args)
break
except Exception:
time.sleep(2 ** attempt)
else:
raise RuntimeError("all retries failed")→ Retry with backoff (L22). Nested inside pagination — each page's call gets its own retry budget. The for / else re-raises only if no break happened.
all_msgs.extend(result.get("messages", []))
page_token = result.get("nextPageToken")
if not page_token:
break→ Defensive read + pagination break (L23 + week 1 L4). .get() returns None/[] on missing keys.
emails = fetch_emails_paginated(pages=2, per_page=5)
filtered = [e for e in emails if e.get("subject")]→ Filter via list comprehension (Py L24). Drops emails missing a subject.
for e in filtered:
print(f"would notify: {e.get('subject')}")
print(f"total notified: {len(filtered)}")→ Batch action (L25). Loop, do something per item, log a count at the end.
Production automation scripts that feel "big" usually aren't doing anything new — they're just stacking patterns. Once each individual pattern is reflexive, reading and writing scripts that compose them gets easy.
print with a real action: send_slack, append_to_sheet.is_unread, from_specific_sender, time-window check (week 3 L18).The shape stays the same. You add layers as the requirements need them.
Last code lesson of the track. We tie five week-4 patterns into one short script:
print per kept email — standing in for an action like send_slack per email (L25).import time
def fetch_emails_paginated(pages=2, per_page=5):
all_msgs, page_token = [], None
for _ in range(pages):
args = {"max_results": per_page}
if page_token:
args["page_token"] = page_token
for attempt in range(1, 4):
try:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, args)
break
except Exception:
time.sleep(2 ** attempt)
else:
raise RuntimeError("all retries failed")
all_msgs.extend(result.get("messages", []))
page_token = result.get("nextPageToken")
if not page_token:
break
return all_msgs
emails = fetch_emails_paginated(pages=2, per_page=5)
filtered = [e for e in emails if e.get("subject")]
for e in filtered:
print(f"would notify: {e.get('subject')}")
print(f"total notified: {len(filtered)}")Every line uses a primitive I learned earlier in the track.
That's the point. No new tools, no new concepts — just composition. This is what "writing automation" looks like in practice: not memorising one big script, but knowing the small patterns well enough to compose them on demand.
No new tool. No new concept. Composition of week-4 + earlier patterns into one shape.
def fetch_emails_paginated(pages=2, per_page=5):→ Helper (L24). Named, reusable, defaults on args.
all_msgs, page_token = [], None
for _ in range(pages):
args = {"max_results": per_page}
if page_token:
args["page_token"] = page_token→ Pagination scaffold (L23). Outer loop bounded by pages; per-iteration args dict.
for attempt in range(1, 4):
try:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, args)
break
except Exception:
time.sleep(2 ** attempt)
else:
raise RuntimeError("all retries failed")→ Retry with backoff (L22). Nested inside pagination — each page's call gets its own retry budget. The for / else re-raises only if no break happened.
all_msgs.extend(result.get("messages", []))
page_token = result.get("nextPageToken")
if not page_token:
break→ Defensive read + pagination break (L23 + week 1 L4). .get() returns None/[] on missing keys.
emails = fetch_emails_paginated(pages=2, per_page=5)
filtered = [e for e in emails if e.get("subject")]→ Filter via list comprehension (Py L24). Drops emails missing a subject.
for e in filtered:
print(f"would notify: {e.get('subject')}")
print(f"total notified: {len(filtered)}")→ Batch action (L25). Loop, do something per item, log a count at the end.
Production automation scripts that feel "big" usually aren't doing anything new — they're just stacking patterns. Once each individual pattern is reflexive, reading and writing scripts that compose them gets easy.
print with a real action: send_slack, append_to_sheet.is_unread, from_specific_sender, time-window check (week 3 L18).The shape stays the same. You add layers as the requirements need them.
Create a free account to get started. Paid plans unlock all tracks.