Python only what you need
Ship and explain the score labeler
Page 8 packages proved vs unproved evidence so another engineer can run, trust, or reject the threshold score labeler (`scores.py`).
1Learn the idea
Read
Assemble the ship record
A shippable lab artifact includes: how to run it, the metric result (boundary labels at 0.49/0.50/0.51 and empty-list positive count 0), the failure you can still reproduce (string scores that compare lexicographically, or IndentationError that hides a wrong cutoff), the security gate for eval() on pasted score text or logging raw learner identifiers beside scores, and a rollback note. The user decision it supports remains: accept or reject a model score using one shared cutoff.
Read
Assemble the runnable artifact
def validate(scores, threshold):
if not isinstance(scores, list):
raise TypeError("scores must be a list")
values = []
for index, value in enumerate(scores):
if isinstance(value, bool) or not isinstance(value, (int, float)):
raise TypeError(f"score[{index}] must be a number")
value = float(value)
if not 0.0 <= value <= 1.0:
raise ValueError(f"score[{index}] must be between 0 and 1")
values.append(value)
if isinstance(threshold, bool) or not isinstance(threshold, (int, float)):
raise TypeError("threshold must be a number")
threshold = float(threshold)
if not 0.0 <= threshold <= 1.0:
raise ValueError("threshold must be between 0 and 1")
return values, threshold
def label_scores(scores, threshold):
scores, threshold = validate(scores, threshold)
labels = ["yes" if score >= threshold else "no" for score in scores]
return labels, labels.count("yes")
assert label_scores([0.49, 0.50, 0.51], 0.50) == (["no", "yes", "yes"], 2)
assert label_scores([], 0.50) == ([], 0)
try:
label_scores(["0.9"], 0.50)
except TypeError as error:
assert str(error) == "score[0] must be a number"
else:
raise AssertionError("numeric strings must be rejected")
print(label_scores([0.2, 0.9, 0.4], 0.5))
print("3/3 release checks passed")
Expected output:
(['no', 'yes', 'no'], 1)
3/3 release checks passed
Save this as scores.py and run python3 scores.py. It uses only the Python standard library.
Read
Explain limits without apology
State operating limits for the score labeler in plain language: fixture size, offline vs live dependencies, and what would require a new eval set. Shipping python-only-what-you-need is honest scoping, not maximal confidence language.
Read
Lab notebook: proved vs unproved
Fill this table in your notes for the score labeler:
- Proved on
scores=[0.2,0.9,0.4], threshold=0.5: … - Unproved beyond the fixture: …
- Metric that blocks release: boundary labels at 0.49/0.50/0.51 and empty-list positive count 0
- Failure still reproducible: string scores that compare lexicographically, or IndentationError that hides a wrong cutoff
- Security gate: eval() on pasted score text or logging raw learner identifiers beside scores
- Rollback: …
Ship the narrative only when the unproved list is honest. Reviewers trust narrow claims that support accept or reject a model score using one shared cutoff more than maximal language that collapses under the first production oddity.
Read
Worked judgment
Hand your ship note to a peer and ask them to recreate a proved/unproved ship note with rollback without watching you type. If they cannot, your evidence is still tribal knowledge. Tighten the run command and the metric line until a stranger can validate the score labeler against scores=[0.2,0.9,0.4], threshold=0.5.
Read
Independent transfer
Ship a temperatures.py artifact with the same structure: explicit contract, one shared boundary rule, empty-input behavior, a rejected string case, aggregate-only logs, and a six-line ship note. A peer should be able to run and review it without this lesson.
Go deeper
Before you start
Why this matters
List two things this chapter proved on the fixture and two things it did not prove about the score labeler. If you cannot name the gaps, you are not ready to ship the narrative—even if the code runs.
In the wild
See how this idea shows up as a product and a company — then come back to the lesson. Skills transfer across vendors.
Related lessons
Check your understanding
Page assessment
Answer from memory. Completion is saved from this evidence, not from opening the next page.
All responses are required.