Sometimes the action depends on the input. A user wants email notifications? Send a Gmail. Wants calendar reminders? Create an event. Wants both? Call both.
Branch on the value, call the appropriate tool. Just if / elif / else from Python L8 — except the body is a tool call:
def notify(channel_type, text):
if channel_type == "email":
toolset.execute_action(Action.GMAIL_SEND_EMAIL, {
"recipient_email": "me@example.com",
"subject": "alert",
"body": text,
})
elif channel_type == "calendar":
toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {
"calendar_id": "primary",
"summary": text,
"start_datetime": "2026-12-31T11:00:00+00:00",
"event_duration_minutes": 15,
})
else:
raise ValueError(f"unknown channel: {channel_type}")
notify("email", "build green")A long if chain feels brittle if I add 5 more channels.
Right — at 4+ branches, switch to a dict-of-functions:
DISPATCH = {
"email": lambda text: toolset.execute_action(Action.GMAIL_SEND_EMAIL, {...}),
"calendar": lambda text: toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {...}),
"task": lambda text: toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {...}),
}
def notify(channel_type, text):
handler = DISPATCH.get(channel_type)
if not handler:
raise ValueError(f"unknown channel: {channel_type}")
handler(text)Adding a channel = adding a dict entry. Less repetition, easier to read. This is the dispatch table pattern.
Branching on a value to choose an action. Two main shapes:
if / elif / else chainFor 2-3 branches, the simple shape is clearest:
def notify(channel_type, text):
if channel_type == "email":
send_email(text)
elif channel_type == "calendar":
create_event(text)
else:
raise ValueError(f"unknown channel: {channel_type}")The else raising is important — silently skipping unknown values hides bugs.
For 4+ branches, a dict is cleaner:
DISPATCH = {
"email": send_email,
"calendar": create_event,
"task": create_task,
"doc": append_doc,
}
def notify(channel_type, text):
handler = DISPATCH.get(channel_type)
if not handler:
raise ValueError(f"unknown channel: {channel_type}")
handler(text)Two patterns to note:
.get() returns None for missing keys — explicit check is clearer than try/except KeyError.The handlers in a dispatch table are functions you defined in the helpers lesson. The pattern composes:
def send_email(text):
return toolset.execute_action(Action.GMAIL_SEND_EMAIL, {
"recipient_email": "me@example.com", "subject": "alert", "body": text,
})
def create_event(text):
return toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {
"calendar_id": "primary", "summary": text,
"start_datetime": "2026-12-31T11:00:00+00:00", "event_duration_minutes": 15,
})
DISPATCH = {"email": send_email, "calendar": create_event}if/else is fine. Don't reach for a dict for 2 entries.send_email(to, subject, body) and create_event(calendar_id, summary, start_datetime), normalise the arg shape first.Sometimes the action depends on the input. A user wants email notifications? Send a Gmail. Wants calendar reminders? Create an event. Wants both? Call both.
Branch on the value, call the appropriate tool. Just if / elif / else from Python L8 — except the body is a tool call:
def notify(channel_type, text):
if channel_type == "email":
toolset.execute_action(Action.GMAIL_SEND_EMAIL, {
"recipient_email": "me@example.com",
"subject": "alert",
"body": text,
})
elif channel_type == "calendar":
toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {
"calendar_id": "primary",
"summary": text,
"start_datetime": "2026-12-31T11:00:00+00:00",
"event_duration_minutes": 15,
})
else:
raise ValueError(f"unknown channel: {channel_type}")
notify("email", "build green")A long if chain feels brittle if I add 5 more channels.
Right — at 4+ branches, switch to a dict-of-functions:
DISPATCH = {
"email": lambda text: toolset.execute_action(Action.GMAIL_SEND_EMAIL, {...}),
"calendar": lambda text: toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {...}),
"task": lambda text: toolset.execute_action(Action.GOOGLETASKS_CREATE_TASK, {...}),
}
def notify(channel_type, text):
handler = DISPATCH.get(channel_type)
if not handler:
raise ValueError(f"unknown channel: {channel_type}")
handler(text)Adding a channel = adding a dict entry. Less repetition, easier to read. This is the dispatch table pattern.
Branching on a value to choose an action. Two main shapes:
if / elif / else chainFor 2-3 branches, the simple shape is clearest:
def notify(channel_type, text):
if channel_type == "email":
send_email(text)
elif channel_type == "calendar":
create_event(text)
else:
raise ValueError(f"unknown channel: {channel_type}")The else raising is important — silently skipping unknown values hides bugs.
For 4+ branches, a dict is cleaner:
DISPATCH = {
"email": send_email,
"calendar": create_event,
"task": create_task,
"doc": append_doc,
}
def notify(channel_type, text):
handler = DISPATCH.get(channel_type)
if not handler:
raise ValueError(f"unknown channel: {channel_type}")
handler(text)Two patterns to note:
.get() returns None for missing keys — explicit check is clearer than try/except KeyError.The handlers in a dispatch table are functions you defined in the helpers lesson. The pattern composes:
def send_email(text):
return toolset.execute_action(Action.GMAIL_SEND_EMAIL, {
"recipient_email": "me@example.com", "subject": "alert", "body": text,
})
def create_event(text):
return toolset.execute_action(Action.GOOGLECALENDAR_CREATE_EVENT, {
"calendar_id": "primary", "summary": text,
"start_datetime": "2026-12-31T11:00:00+00:00", "event_duration_minutes": 15,
})
DISPATCH = {"email": send_email, "calendar": create_event}if/else is fine. Don't reach for a dict for 2 entries.send_email(to, subject, body) and create_event(calendar_id, summary, start_datetime), normalise the arg shape first.Create a free account to get started. Paid plans unlock all tracks.