Read 5 messages. Create a Calendar event for any whose subject contains the word urgent. Day 4's filtering pattern + day 9's create + a counter to verify how many fired.
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5})
messages = result.get("messages", [])
sent_count = 0
for m in messages:
subject = m.get("subject", "")
if "urgent" in subject.lower():
toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {
"calendar_id": "primary",
"summary": f"Urgent: {subject}",
"start_datetime": "2026-12-31T09:00:00+00:00",
"event_duration_minutes": 15,
})
sent_count += 1
print(f"sent {sent_count} alerts")subject.lower() — to make the match case-insensitive?
Right. Real subjects might be URGENT or Urgent or urgent. Lowercasing both sides of the comparison normalizes. Without it you'd miss Urgent: Q4 Report.
And nothing fires if no message matches?
That's the design. No matching subject = no event created. sent_count == 0 means nothing matched — which is correct behaviour, not a bug.
The shape:
for item in items:
if matches_condition(item):
do_side_effect(item)The filter and the side effect are tied together — neither makes sense without the other in this lesson. (Pure filter without side effect = day 4. Side effect on every item = day 8. Together = day 17.)
Real-world strings are messy. "URGENT: ...", "Urgent: ...", "urgent: ..." — same intent, different casing. Normalize:
if "urgent" in subject.lower(): # always works regardless of how the user typed itFor more sophisticated matching (whole word, regex), reach for the re module. For one-keyword case-insensitive, lowercase + in is enough.
sent_count = 0
for item in items:
if matches:
side_effect(item)
sent_count += 1 # the `+= 1` is shorthand for `sent_count = sent_count + 1`Reporting how many side effects fired is good practice. If a script is supposed to alert five customers and the count is 0, that's an immediate signal something's off.
When the list is empty (or nothing matches), the loop body never runs. No side effect. No noise. That's correct — "only alert when urgent" means "don't alert when not urgent". Don't add a fallback print("no urgent items") unless you actually want that signal.
Read 5 messages. Create a Calendar event for any whose subject contains the word urgent. Day 4's filtering pattern + day 9's create + a counter to verify how many fired.
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5})
messages = result.get("messages", [])
sent_count = 0
for m in messages:
subject = m.get("subject", "")
if "urgent" in subject.lower():
toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {
"calendar_id": "primary",
"summary": f"Urgent: {subject}",
"start_datetime": "2026-12-31T09:00:00+00:00",
"event_duration_minutes": 15,
})
sent_count += 1
print(f"sent {sent_count} alerts")subject.lower() — to make the match case-insensitive?
Right. Real subjects might be URGENT or Urgent or urgent. Lowercasing both sides of the comparison normalizes. Without it you'd miss Urgent: Q4 Report.
And nothing fires if no message matches?
That's the design. No matching subject = no event created. sent_count == 0 means nothing matched — which is correct behaviour, not a bug.
The shape:
for item in items:
if matches_condition(item):
do_side_effect(item)The filter and the side effect are tied together — neither makes sense without the other in this lesson. (Pure filter without side effect = day 4. Side effect on every item = day 8. Together = day 17.)
Real-world strings are messy. "URGENT: ...", "Urgent: ...", "urgent: ..." — same intent, different casing. Normalize:
if "urgent" in subject.lower(): # always works regardless of how the user typed itFor more sophisticated matching (whole word, regex), reach for the re module. For one-keyword case-insensitive, lowercase + in is enough.
sent_count = 0
for item in items:
if matches:
side_effect(item)
sent_count += 1 # the `+= 1` is shorthand for `sent_count = sent_count + 1`Reporting how many side effects fired is good practice. If a script is supposed to alert five customers and the count is 0, that's an immediate signal something's off.
When the list is empty (or nothing matches), the loop body never runs. No side effect. No noise. That's correct — "only alert when urgent" means "don't alert when not urgent". Don't add a fallback print("no urgent items") unless you actually want that signal.
Create a free account to get started. Paid plans unlock all tracks.