Page 6 of 8~96 min topic

Python only what you need

Instrument the score labeler

Page 6 adds signals that distinguish bad input from component failure in the threshold score labeler (`scores.py`).

~12 min this pageTesting and observabilityReviewed 2026-08-08

1Learn the idea

Read

Emit stage signals

Instrument the threshold score labeler (scores.py) so a run records enough structure to debug offline: counts, latency if relevant, pass/fail of boundary labels at 0.49/0.50/0.51 and empty-list positive count 0, and a stable stage name. Redact secrets and raw credentials from every event.

Read

Emit and assert

import json

def make_event(labels, threshold):
    return {
        "stage": "label_scores",
        "input_count": len(labels),
        "threshold": threshold,
        "positive_count": labels.count("yes"),
        "ok": True,
    }

event = make_event(["no", "yes", "no"], 0.5)
print(json.dumps(event, sort_keys=True))

Expected output:

{"input_count": 3, "ok": true, "positive_count": 1, "stage": "label_scores", "threshold": 0.5}

The event records counts, not raw scores or learner identifiers. A validation failure should emit a separate safe event such as {"stage":"validate","ok":false,"error_type":"TypeError","field":"score[0]"}—never the rejected value.

Read

Lock signals with a regression test

Turn one historical failure—especially string scores that compare lexicographically—into a test that fails if the signal disappears for the score labeler. Observability without a failing test is optional decoration; observability with a test is part of the python-only-what-you-need artifact.

Read

Lab notebook: signal schema

Draft a three-field event for the score labeler: stage, ok, and one domain field derived from boundary labels at 0.49/0.50/0.51 and empty-list positive count 0. Add fixture_id or docs_version when content can change. Explicitly list fields that must never appear (tokens, passwords, raw prompts) because eval() on pasted score text or logging raw learner identifiers beside scores is in scope for this lab.

Wire one assertion that fails if the score labeler event is missing after a run. Observability that cannot fail a test will not survive contact with a busy python-only-what-you-need repository.

Read

Worked judgment

Imagine a teammate opens only your event stream after a bad deploy. Could they tell whether scores=[0.2,0.9,0.4], threshold=0.5 was wrong, whether string scores that compare lexicographically, or IndentationError that hides a wrong cutoff returned, or whether eval() on pasted score text or logging raw learner identifiers beside scores slipped through? If not, rename fields until those three stories are distinguishable.

Read

Guided practice

Assert event["input_count"] == 3, event["positive_count"] == 1, and that forbidden keys scores, learner_id, and email are absent. Then design the corresponding safe validation-failure event.

Read

Independent transfer

Instrument a pass/fail attendance counter. Log aggregate counts and a stage name while proving that student names never appear in the event.

ML Python starter

Previous · Next

Go deeper

Before you start

Why this matters

Write the single log line or metric event that would tell you whether a bad result came from input vs implementation for the score labeler. If your line could not tell them apart, redesign it before coding.

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.

Check your understanding

Page assessment

Answer from memory. Completion is saved from this evidence, not from opening the next page.

1. Can input faults be distinguished from component faults in the event?
2. Are secrets redacted from logs?
3. Is there a test that fails if the signal vanishes?
4. Does the event still reference the decision: accept or reject a model score using one shared cutoff?

All responses are required.