You have two API counts sitting in variables. Now you want one function to tell you which source has more things in it — emails or events. What is the smallest conditional that answers that?
Two counts and a compare? Something like if email_count > event_count: return "emails" and the opposite for events? What do I return when they're equal?
Three possible labels — "emails", "events", or "tie". The two API reads are the usual pattern, and the decision is a simple if/elif/else:
email_count = len(emails.get("messages", []))
event_count = len(events.get("items", []))So the function does three things — read Gmail, read Calendar, compare. Is there a pattern to keep the compare readable once the counts are computed?
Keep each stage on its own line: fetch, count, decide. The decision block reads top-to-bottom with no magic:
def bigger_source(max_each: int) -> str:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_each})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
ec = len(emails.get("messages", []))
vc = len(events.get("items", []))
if ec > vc: return "emails"
if vc > ec: return "events"
return "tie"What if both are zero? That's technically a tie, but it feels like a separate case — is there anything wrong with lumping it in?
0 == 0 is a tie by the rule, so the function is consistent. If the caller cares about the empty-morning case they can check the counts themselves. Keep this function single-purpose — return the label, nothing else.
So in one function I'm making two live API calls and deriving a verdict from them — this is the same shape I'll use any time I compare two sources?
The exact shape. Two reads, two counts, one decision string. Swap APIs and you still have a bigger-source function; swap the compare for a sum and you have a total. The skeleton is the reusable part.
TL;DR: Fetch both, count both, branch on the compare — return a single label.
GMAIL_FETCH_EMAILS + GOOGLECALENDAR_FIND_EVENTlen(.get(key, [])) on each responseif/elif/else returning "emails", "events", or "tie"| Return type | Caller experience |
|---|---|
| Number (diff) | Must know sign + handle zero |
| Label string | Branches on a word, zero ambiguity |
Labels keep decisions explicit. Callers never have to remember what a negative number means or who came first in the compare.
You have two API counts sitting in variables. Now you want one function to tell you which source has more things in it — emails or events. What is the smallest conditional that answers that?
Two counts and a compare? Something like if email_count > event_count: return "emails" and the opposite for events? What do I return when they're equal?
Three possible labels — "emails", "events", or "tie". The two API reads are the usual pattern, and the decision is a simple if/elif/else:
email_count = len(emails.get("messages", []))
event_count = len(events.get("items", []))So the function does three things — read Gmail, read Calendar, compare. Is there a pattern to keep the compare readable once the counts are computed?
Keep each stage on its own line: fetch, count, decide. The decision block reads top-to-bottom with no magic:
def bigger_source(max_each: int) -> str:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_each})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
ec = len(emails.get("messages", []))
vc = len(events.get("items", []))
if ec > vc: return "emails"
if vc > ec: return "events"
return "tie"What if both are zero? That's technically a tie, but it feels like a separate case — is there anything wrong with lumping it in?
0 == 0 is a tie by the rule, so the function is consistent. If the caller cares about the empty-morning case they can check the counts themselves. Keep this function single-purpose — return the label, nothing else.
So in one function I'm making two live API calls and deriving a verdict from them — this is the same shape I'll use any time I compare two sources?
The exact shape. Two reads, two counts, one decision string. Swap APIs and you still have a bigger-source function; swap the compare for a sum and you have a total. The skeleton is the reusable part.
TL;DR: Fetch both, count both, branch on the compare — return a single label.
GMAIL_FETCH_EMAILS + GOOGLECALENDAR_FIND_EVENTlen(.get(key, [])) on each responseif/elif/else returning "emails", "events", or "tie"| Return type | Caller experience |
|---|---|
| Number (diff) | Must know sign + handle zero |
| Label string | Branches on a word, zero ambiguity |
Labels keep decisions explicit. Callers never have to remember what a negative number means or who came first in the compare.
Create a free account to get started. Paid plans unlock all tracks.