Complete worked example
python
import numpy as np
def inspect_example(records, country, limit=2):
def valid_country(value):
return type(value) is str and len(value) == 2 and value.isascii() and value.isalpha() and value.isupper()
if not valid_country(country) or type(limit) is not int or not 1 <= limit <= 20:
raise ValueError("expected a country code and bounded display limit")
required = {"id", "country", "score", "technical"}
for row in records:
if type(row) is not dict or set(row) != required:
raise ValueError("unexpected record structure")
if type(row["id"]) is not int or row["id"] < 1 or not valid_country(row["country"]):
raise ValueError("invalid canonical identity or country")
if type(row["technical"]) is not bool or (row["score"] is not None and type(row["score"]) not in (int, float)):
raise ValueError("invalid technical flag or numerical score")
ids = np.asarray([row["id"] for row in records], dtype=np.int64)
if np.unique(ids).size != ids.size:
raise ValueError("duplicate canonical identifier")
countries = np.asarray([row["country"] for row in records], dtype=np.str_)
technical = np.asarray([row["technical"] for row in records], dtype=np.bool_)
scores = np.asarray([row["score"] for row in records], dtype=np.float64)
mask = (countries == country) & technical & np.isfinite(scores) & (scores >= 0)
selected_ids = ids[mask]
values = scores[mask]
order = np.lexsort((selected_ids, -values))
return {"country": country, "count": int(values.size),
"meanScore": float(values.mean()) if values.size else None,
"topIds": selected_ids[order][:limit].tolist()}- Reasoning
- The worked function validates structural fields before constructing aligned arrays. It then creates one combined mask for country, technical eligibility, finite score and nonnegative scope. It applies that mask to both identifiers and scores. A shared lexsort permutation ranks decreasing scores with increasing identifier tie-breaks, and only the displayed top identifier list is limited. The mean and count still describe every eligible observation. This order matters: validate, select, summarize the selected population, rank aligned identities, then bound the presentation. A page-size limit is not a sampling design and must not quietly become the denominator for a whole-population report.