Your calendar has events for this week — standups, reviews, one-on-ones. You want the first event that matches a keyword like review. How would the search call look?
Same toolset.execute_action skeleton? Pass a query in the params dict, get a list back, grab the first item?
Exactly right. The action is GOOGLECALENDAR_FIND_EVENT, the query goes in as {"query": "review"}, and the response puts matching events under items. Pull the first one or fall back to an empty dict if nothing matches:
result = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": "review"})
events = result.get("items", [])
first = events[0] if events else {}That ternary — events[0] if events else {} — reads like "give me the first one if there are any, otherwise give me an empty dict"?
Exactly. It's the one-line version of the early return pattern. Compact and safe — no IndexError, caller always gets a dict:
def find_event(query: str) -> dict:
result = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": query})
events = result.get("items", [])
return events[0] if events else {}An empty dict instead of None? Why not None?
Matching the return type makes the caller's life easier. If you always return a dict, the caller can safely do .get("summary", "") without checking for None first. Same-shape returns beat mixed-type returns every time.
So every caller of find_event can just read .get("summary") or .get("start") and trust it will not crash — whether the query matched or not?
That is the whole point of returning a safe shape. Your function is a stable building block now. Week 3 will stack several of these together, and the guarantees you put in today let tomorrow's code stay simple.
TL;DR: events[0] if events else {} grabs the first item or a safe default in one line.
Action.GOOGLECALENDAR_FIND_EVENT{"query": "review"}{"items": [...]}| Return shape | Caller code |
|---|---|
dict or None | needs if result is not None check |
dict always | result.get("summary", "") just works |
Your calendar has events for this week — standups, reviews, one-on-ones. You want the first event that matches a keyword like review. How would the search call look?
Same toolset.execute_action skeleton? Pass a query in the params dict, get a list back, grab the first item?
Exactly right. The action is GOOGLECALENDAR_FIND_EVENT, the query goes in as {"query": "review"}, and the response puts matching events under items. Pull the first one or fall back to an empty dict if nothing matches:
result = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": "review"})
events = result.get("items", [])
first = events[0] if events else {}That ternary — events[0] if events else {} — reads like "give me the first one if there are any, otherwise give me an empty dict"?
Exactly. It's the one-line version of the early return pattern. Compact and safe — no IndexError, caller always gets a dict:
def find_event(query: str) -> dict:
result = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": query})
events = result.get("items", [])
return events[0] if events else {}An empty dict instead of None? Why not None?
Matching the return type makes the caller's life easier. If you always return a dict, the caller can safely do .get("summary", "") without checking for None first. Same-shape returns beat mixed-type returns every time.
So every caller of find_event can just read .get("summary") or .get("start") and trust it will not crash — whether the query matched or not?
That is the whole point of returning a safe shape. Your function is a stable building block now. Week 3 will stack several of these together, and the guarantees you put in today let tomorrow's code stay simple.
TL;DR: events[0] if events else {} grabs the first item or a safe default in one line.
Action.GOOGLECALENDAR_FIND_EVENT{"query": "review"}{"items": [...]}| Return shape | Caller code |
|---|---|
dict or None | needs if result is not None check |
dict always | result.get("summary", "") just works |
Create a free account to get started. Paid plans unlock all tracks.