Your scripts now call toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": N}) repeatedly. Same shape, different N. Three calls in one script — three near-identical lines.
Wrap it in a function. The shape from Python Beginner L17 — def name(params): ...:
def fetch_emails(n=10):
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": n})
return result.get("messages", [])
recent = fetch_emails(5)
lots = fetch_emails(50)Three tool-call lines collapsed into one helper definition + two call sites. If the tool's argument shape ever changes (max_results becomes limit), you fix it in one place.
When should I extract a helper?
Three signals.
.get("messages", []) to avoid KeyError. Hide that inside the helper.fetch_emails(5) reads better than toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5}).Helpers just for me, or shared somewhere?
For now, just at the top of your script. As you build more, you'll move them to a shared file you import — but that's Python Intermediate. Today: define and use within one script.
A helper is a function you define to name a piece of work. Inside the function, the tool-call boilerplate. Outside, a clean call by name:
def fetch_emails(n=10):
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": n})
return result.get("messages", [])
def send_message(channel, text):
return toolset.execute_action(Action.SLACK_SEND_MESSAGE, {"channel": channel, "text": text})
def append_to_sheet(sheet_id, row):
return toolset.execute_action(Action.GOOGLESHEETS_BATCH_UPDATE, {
"spreadsheet_id": sheet_id,
"values": [row],
})Now your script reads:
emails = fetch_emails(5)
for email in emails:
send_message("#updates", email["subject"])
append_to_sheet(SHEET_ID, [email["from"], email["subject"]])Not one mention of toolset.execute_action in the main flow. The plumbing is in the helpers; the intent is in the calls.
def fetch_emails(n=10, query=None):
args = {"max_results": n}
if query:
args["q"] = query
return toolset.execute_action(Action.GMAIL_FETCH_EMAILS, args).get("messages", [])
fetch_emails() # 10 most recent
fetch_emails(50) # 50 most recent
fetch_emails(query="is:unread") # most recent unreadDefault values + optional kwargs — the call site stays clean for common cases.
Helpers compose with everything you've learned:
A mature script's helpers are 3-5 lines each, but each one bundles a defensive read (.get(...)), a retry loop, and a log line. The main script reads like English.
fetch_emails(5) is no clearer than the inline tool call, skip it.Your scripts now call toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": N}) repeatedly. Same shape, different N. Three calls in one script — three near-identical lines.
Wrap it in a function. The shape from Python Beginner L17 — def name(params): ...:
def fetch_emails(n=10):
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": n})
return result.get("messages", [])
recent = fetch_emails(5)
lots = fetch_emails(50)Three tool-call lines collapsed into one helper definition + two call sites. If the tool's argument shape ever changes (max_results becomes limit), you fix it in one place.
When should I extract a helper?
Three signals.
.get("messages", []) to avoid KeyError. Hide that inside the helper.fetch_emails(5) reads better than toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5}).Helpers just for me, or shared somewhere?
For now, just at the top of your script. As you build more, you'll move them to a shared file you import — but that's Python Intermediate. Today: define and use within one script.
A helper is a function you define to name a piece of work. Inside the function, the tool-call boilerplate. Outside, a clean call by name:
def fetch_emails(n=10):
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": n})
return result.get("messages", [])
def send_message(channel, text):
return toolset.execute_action(Action.SLACK_SEND_MESSAGE, {"channel": channel, "text": text})
def append_to_sheet(sheet_id, row):
return toolset.execute_action(Action.GOOGLESHEETS_BATCH_UPDATE, {
"spreadsheet_id": sheet_id,
"values": [row],
})Now your script reads:
emails = fetch_emails(5)
for email in emails:
send_message("#updates", email["subject"])
append_to_sheet(SHEET_ID, [email["from"], email["subject"]])Not one mention of toolset.execute_action in the main flow. The plumbing is in the helpers; the intent is in the calls.
def fetch_emails(n=10, query=None):
args = {"max_results": n}
if query:
args["q"] = query
return toolset.execute_action(Action.GMAIL_FETCH_EMAILS, args).get("messages", [])
fetch_emails() # 10 most recent
fetch_emails(50) # 50 most recent
fetch_emails(query="is:unread") # most recent unreadDefault values + optional kwargs — the call site stays clean for common cases.
Helpers compose with everything you've learned:
A mature script's helpers are 3-5 lines each, but each one bundles a defensive read (.get(...)), a retry loop, and a log line. The main script reads like English.
fetch_emails(5) is no clearer than the inline tool call, skip it.Create a free account to get started. Paid plans unlock all tracks.