← Complete program

Chapter 13 · Course 3 c13-m03

What should we do with doubtful data?

Your goal Separate accepted and doubtful messages while preserving originals, then process a traceable correction.

The lesson, the vocabulary, the worked example and the mission are separate blocks. You can follow them in order or return to the one you need.

Video explanation

This English explanation has English subtitles. Its examples may use a separate practice dataset; use the data stated in the written mission for your answers.

01

Story · optional context

The story so far

You are Aster Motors' new data recruit. After Luca's crash, Noctis 01 is missing and Voss uses an authorization bearing your name to accuse you. Mara Sol, your data lead, and Imani measured a defect: operator fields are mostly absent from transfers in the small supplier batch. You must stop a report from presenting those rows as complete while retaining the original files. TRANSFER-7's page matters especially: throwing it away loses a clue, while filling it yourself manufactures evidence.

This lesson stands alone. Data quarantine is a separate place for deliveries that fail checks. It retains raw content, source, delivery ID, and error code. A later correction is a new delivery linked to the old one; it does not erase the original defect. You will follow three fictional messages and one correction without touching real data.

02

Before the lesson

Purpose of this course

Separate accepted and doubtful messages while preserving originals, then process a traceable correction.

Topics covered

Explain why an invalid row stays available in raw form.; Distinguish unreadable JSON, wrong shape, and missing operator_id.; Read routing to accepted or quarantine with an error code.; Link a correction to the rejected delivery without rewriting history.

What you will learn

  • Explain why an invalid row stays available in raw form.
  • Distinguish unreadable JSON, wrong shape, and missing operator_id.
  • Read routing to accepted or quarantine with an error code.
  • Link a correction to the rejected delivery without rewriting history.

PART 1 · THE LESSON

Understand step by step

1

Lesson · explanation

Quarantine protects a clue

You are Aster Motors' new data recruit. Luca was injured, Noctis 01 vanished, and Voss uses an authorization bearing your name to accuse you. Imani and Mara found that the supplier often omits the operator field on transfers in the small batch. The TRANSFER-7 page has this defect. Voss wants it deleted before the meeting; Mara refuses to lose a clue.

A pipeline is a program that reads deliveries and prepares a report. It must not treat an incomplete row as complete. Quarantine is a separate area for a delivery that fails a check but remains available for inspection. Picture a damaged parcel placed on an examination shelf: it is not sold, yet it is not thrown away.

Quarantine does not mean that the supplier lied or the operator is guilty. It means the delivery does not meet known rules. For TRANSFER-7, inventing an operator_id yourself would manufacture evidence, while deleting the page would hide what arrived. Preserve its exact contents and request a correction you can trace.

Counterexample: remove the doubtful row from both report and archive, then announce that every received transfer was complete. The report would lose a clue and conceal its scope. Separate usable data from the material under examination, count both groups, and say what the accepted part does not cover.

Pause and think

Why keep the incomplete TRANSFER-7 delivery?

See the explanation

Its raw content records what arrived; deleting or filling it yourself would destroy that evidence.

2

Lesson · explanation

A delivery is not the event it describes

Each received message has a delivery_id such as a1, a2, a3, or a4. It names the delivery, like a parcel tracking number. Inside JSON, event_id names the described event, such as E8 or TRANSFER-7. Two parcels can discuss one event: a2 and its correction a4 both concern TRANSFER-7 but remain separate deliveries.

For a quarantined entry, keep four things: delivery_id, source_file, raw_payload, and error_code. They identify one message, name the received file, preserve its exact text, and explain rejection. It is like attaching a tracking label next to a damaged parcel without changing what is inside. These fields make a later comparison possible.

a2's raw_payload is a JSON object containing event_id for TRANSFER-7 but no operator_id. a3's text stops after the event_id key and is not a complete JSON document. Keep both texts as delivered so an investigator can compare them with supplier files. Tidying up their spelling or adding missing fields would change the evidence.

Counterexample: count four delivery_id values and announce four physical transfers. a4 may correct a2 about the same event_id. Message counts and event counts answer different questions. A real system also records receipt times and fingerprints if available, restricts access, and sets retention rules; this lesson uses fictional values.

Pause and think

Are a2 and a4 the same delivery because both discuss TRANSFER-7?

See the explanation

No. They have different delivery IDs; a4 proposes to correct a2 for one event.

3

Lesson · explanation

First ask whether the JSON can be read

JSON writes structured data with keys and values. In Python, json.loads(raw_payload) tries to read the text. If quotes, braces, or values are cut off, parsing fails with json.JSONDecodeError. That happens to a3: its text ends after event_id and lacks even a complete value. The program cannot obtain an event object from it.

The program sends a3 to quarantine with BAD_JSON, keeping the truncated text so Imani can see the actual break. Think of a letter torn halfway through a sentence. Guessing the ending and claiming you read the whole message would be unsafe. The precise request to the supplier is for a complete, readable JSON document.

JSON can be syntactically valid yet have the wrong shape. For example, [1, 2] is a valid JSON list, while this pipeline expects an object with keys such as event_id and operator_id. The check isinstance(event, dict) identifies that case as BAD_SHAPE. The order matters: read the text first, then examine its structure.

Counterexample: call a3 MISSING_OPERATOR merely because its broken text does not display that key. The object cannot even be parsed; the key might have been lost in the cutoff. Its first observable error is BAD_JSON. A precise code prevents asking the supplier for an operator when the immediate need is a complete document.

Pause and think

Why is a3 BAD_JSON, rather than MISSING_OPERATOR?

See the explanation

Its truncated text cannot be parsed, so the program cannot inspect the fields of an object.

4

Lesson · explanation

Then check the operator's presence and value

After reading a JSON object, the small program looks for operator_id. a1 describes E8 and contains operator_id = 'OP-8', a nonempty string, so it passes this reduced check. a2 describes TRANSFER-7 but has no operator_id key. It gets MISSING_OPERATOR and does not enter accepted.

Presence and quality are separate questions. An object with operator_id present but set to a number, an empty string, or only spaces is unusable here. The code checks isinstance(event['operator_id'], str) and then event['operator_id'].strip(); failing this produces BAD_OPERATOR. Picture a form whose name box contains only spaces: the box exists, but no usable name was supplied.

These codes help ask the supplier for the right repair. For a2, Mara can say 'the key is absent'; for BAD_OPERATOR, 'the key exists but its value is not nonempty text'. Do not merge these cases. Messages remain separate with sources and reasons, making it possible to compare defects in the small batch.

Counterexample: automatically fill a missing operator with OP-17 because another file hints at it. That would display an inference as a received value. The demonstration also does not check the whole version 2 supplier contract. A real pipeline must check other fields, contract version, and business rules before publishing.

Pause and think

How do MISSING_OPERATOR and BAD_OPERATOR differ?

See the explanation

The first means the key is absent; the second means its present value has an invalid type or is empty.

5

Lesson · explanation

Follow the first three routes and report counts

The program has two lists: accepted for deliveries passing its demonstration checks, and quarantine for failures. Function route reads each message and chooses a destination. a1, with E8 and OP-8, enters accepted. a2, with TRANSFER-7 but no operator, enters quarantine as MISSING_OPERATOR. Truncated a3 enters it as BAD_JSON.

After these three deliveries, len(accepted) is 1 and len(quarantine) is 2. Those are delivery counts, not proven counts of people or road events. A report built from accepted alone sees only a1 at this point. To be honest, it must state that two received deliveries, including TRANSFER-7's, are excluded from its clean calculation.

Imagine a class with three assignments: one readable and complete, one readable but nameless, and one torn. The teacher can grade the first, hold the others for review, and say how many remain. The teacher should not describe the results as if all three had been graded. The rejection count shows Imani the visible size of the problem.

Counterexample: display only 'one accepted transfer' while hiding the two rejected messages. A reader might believe no other page arrived. Keeping a2 and a3 with their codes makes each message's path checkable. Mara never treats 'not published' as if it meant 'never received'.

Pause and think

What are the counts after a1, a2, and a3?

See the explanation

One accepted delivery (a1) and two quarantined deliveries (a2 and a3).

6

Lesson · explanation

a4 corrects a2 without erasing a2

Later the supplier delivers a4, a new object claiming event_id = 'TRANSFER-7', operator_id = 'OP-17', and correction_of = 'a2'. The program checks that correction_of names rejected delivery a2 and that the event_id agrees. It then routes a4, which passes the reduced shape checks. A resolution link a2 → a4 records the correction's history.

Think of an assignment handed in without a name, followed by a second sheet bearing a name and saying 'corrects my earlier work'. You do not throw away the first sheet; it records the original defect. a2 keeps MISSING_OPERATOR and its raw payload. a4 keeps its own delivery ID and its OP-17 claim. The link explains why both exist.

After a4, accepted contains two deliveries, a1 and a4. quarantine still contains historical a2 and a3. There are four processed messages, which does not establish four different road events. Reporting should consider the corrected value only after further checks: expected source, full contract, and agreement with independent traces.

Counterexample: remove a2 from quarantine and edit its raw text to include OP-17. The original flaw disappears and nobody can compare what the supplier first sent. A correction is a new delivery, not an eraser for the past. If a4's link or source is doubtful, keep it under examination.

Pause and think

After a4, how many deliveries are accepted and how many remain in historical quarantine?

See the explanation

Two accepted (a1, a4) and two quarantined (a2, a3); a2 is not deleted.

7

Lesson · explanation

Correct shape does not prove the real actor

The exercise code checks that a4 is a JSON object, operator_id is nonempty text, and its reference points to a2 with the same event_id. It does not check a supplier signature or the physical identity of the person called OP-17. That value is a statement in a message, not a camera showing who initiated the transfer.

For the investigation, Imani must compare a4 with original files, the expected source, access records, times, and independent traces. A disputed correction can remain under review. Quarantine protects the report from presenting a claim as certain too early while keeping the clue available to discover what really happened.

Mara can tell the jury: 'a2 was readable but lacked an operator; a4 claims to repair it and passes the reduced checks; we preserve both versions.' She can add the counts: one accepted and two quarantined first, then two accepted and two historical quarantined. Those statements are stronger than an accusation made under Voss's pressure.

Counterexample: announce 'OP-17 truly made the transfer because a4 was accepted'. Acceptance only means its form met the rules checked here. Despite the story's urgency, this exercise has no real timer. Distinguish a received value, a declared correction, and an identity established with independent evidence.

Pause and think

Does accepting a4 prove OP-17 initiated TRANSFER-7?

See the explanation

No. The program accepts a correction's form and link; real identity requires other evidence.

Visual guide

The four deliveries' path

  1. 1a1 complete E8 → accepted
  2. 2a2 missing operator and a3 cut-off JSON → quarantined with reasons
  3. 3a4 corrects a2 → accepted after checks; a2 remains

Delivery counts and event counts differ; each version keeps its source.

Code · read each line

Route and correct without overwriting

import json

delivered = [
    ("a1", "supplier-page3.json", '{"event_id":"E8","operator_id":"OP-8"}'),
    ("a2", "supplier-page2.json", '{"event_id":"TRANSFER-7"}'),
    ("a3", "supplier-page4.json", '{"event_id":'),
]
accepted = []
quarantine = []

def route(delivery_id, source_file, raw_payload):
    try:
        event = json.loads(raw_payload)
    except json.JSONDecodeError:
        quarantine.append((delivery_id, source_file, raw_payload, "BAD_JSON"))
        return
    if not isinstance(event, dict):
        quarantine.append((delivery_id, source_file, raw_payload, "BAD_SHAPE"))
    elif "operator_id" not in event:
        quarantine.append((delivery_id, source_file, raw_payload, "MISSING_OPERATOR"))
    elif not isinstance(event["operator_id"], str) or not event["operator_id"].strip():
        quarantine.append((delivery_id, source_file, raw_payload, "BAD_OPERATOR"))
    else:
        accepted.append((delivery_id, event))

for delivery in delivered:
    route(*delivery)
print(len(accepted), len(quarantine))
print([(item[0], item[3]) for item in quarantine])

correction = (
    "a4", "supplier-correction.json",
    '{"event_id":"TRANSFER-7","operator_id":"OP-17","correction_of":"a2"}'
)
original = quarantine[0]  # a2 in this small exercise
new_event = json.loads(correction[2])
if (new_event.get("correction_of") == original[0]
        and new_event.get("event_id") == json.loads(original[2]).get("event_id")):
    route(*correction)
    resolution = {"rejected_delivery_id": original[0], "new_delivery_id": correction[0]}
else:
    quarantine.append((*correction, "BAD_CORRECTION_LINK"))
    resolution = None
print(len(accepted), len(quarantine))
print(resolution)

The script retains every rejected raw_payload with its error code. It prints 1 accepted and 2 quarantined, then the reasons for a2 and a3. After new delivery a4, it prints 2 accepted and still 2 quarantined, plus the a2 → a4 link. A real check also needs source authentication and the full contract.

PART 2 · WATCH IT WORK

A complete worked example

Follow each step, then the result

Worked example: correcting TRANSFER-7

a2 contains event TRANSFER-7 without operator_id. The supplier later sends a4 with operator_id OP-17 and correction_of a2.

  1. On a2's arrival, save its exact text and mark MISSING_OPERATOR. Do not insert it in a clean report.
  2. On a4's arrival, verify the expected key and its reference to rejected delivery a2.
  3. Keep a2 and a4 separately, then note a resolution link. Use the new value in reporting only after other checks.
  4. State that the new value is claimed by the supplier; its truth and the person's identity remain unproven.
What this shows

Repairing a result must not erase evidence of the earlier defect.

How this helps with your mission

Voss asks to delete the awkward page, but Mara gives you practice deliveries. Route a1 to accepted, a2 to quarantine with MISSING_OPERATOR, and a3 to quarantine with BAD_JSON. For each rejection preserve exact delivery_id, source_file, raw_payload, and error_code. The first count is 1 accepted and 2 quarantined. When a4 declares OP-17 for TRANSFER-7 with correction_of = a2, check its link and shape, then record a2 → a4 without deleting a2. The count becomes 2 accepted and 2 historical quarantined. Imani still must verify a4's authenticity and the operator's real identity.

PART 3 · YOUR TURN

Use the lesson to solve the mission

Story scene · practical task

Mission: preserve the awkward page

Voss asks for malformed TRANSFER-7 to be deleted before the meeting. Mara refuses: that page may explain why your name appears elsewhere. Imani needs a delivery record showing what was received, rejected, and corrected. You have complete a1, a2 without operator, cut-off JSON a3, and a4 offered as a correction to a2. The urgency is fictional; the exercise has no timer.

Your task

  1. Classify a1, a2, and a3 and give each rejection's error code.
  2. List four items to retain for a quarantined entry.
  3. Explain what changes and what remains after a4 arrives.
  4. Say why accepting a4's shape does not prove OP-17 actually initiated the transfer.

How to know you have succeeded

  • a1 accepted; a2 MISSING_OPERATOR; a3 BAD_JSON.
  • delivery_id, source_file, raw_payload, and error_code remain available.
  • a4 is a new delivery linked to a2; a2 is not erased.
  • The supplier claims OP-17, but source and actor require corroboration.
Open this mission in the browser workspace

Try code or SQL and keep a local draft. The mission questions are checked separately below.

Check the facts in your mission

Answer each factual question, then write your report in your own words. The program checks the choices; compare the report with the worked answer below.

1. Which routing of the first three deliveries is correct?
2. Which four fields should be retained for a quarantined delivery?
3. Readable JSON has `operator_id`, but its value is a string of spaces only. Which code does this reduced check give?
4. After a4 linked to a2 arrives, what are the delivery counts and what happens to a2?
5. What does accepting a4's shape with `operator_id = 'OP-17'` prove?
See the worked answer
  1. a1 has readable JSON and operator_id, so it passes the reduced check. a2 is readable but lacks operator_id: MISSING_OPERATOR. a3 is syntactically cut off: BAD_JSON.
  2. For a2 and a3, record delivery ID, source file, exact original text, and rejection reason. A real system can add received time and fingerprint.
  3. After checking, a4 is accepted and its resolution link names a2. Two deliveries are accepted and two older ones remain quarantined; history has not been rewritten.
  4. The code examines the new message's shape only. It validates neither a supplier signature nor the operator's physical identity. Imani must compare the claim with independent evidence.

You can protect doubtful rows and trace a correction. The last course in this chapter will block publication when a contract change breaks these checks.

Before you leave

Remember this

  • Quarantine keeps a doubtful delivery available while excluding it from a report presented as clean.
  • delivery_id names a received message; event_id names the described event and may recur in a correction.
  • For rejection, retain exact delivery_id, source_file, raw_payload, and error_code.
  • a1 is accepted; a2 lacks operator_id (MISSING_OPERATOR); a3 is unreadable (BAD_JSON).
  • Valid JSON in the wrong shape gets BAD_SHAPE; a present but invalid operator gets BAD_OPERATOR.
  • a4 is a new delivery linked to a2; afterward 2 deliveries are accepted and 2 remain historical quarantine entries.
  • Accepting a correction's shape proves neither its source's authenticity nor OP-17's real identity.
A–Z

Part of the lesson · keep nearby

New words, explained

These are the names of ideas used in the lesson above. Return here whenever a word feels unfamiliar.

Quarantine
Separate area for nonconforming deliveries while they are examined.
Raw data
Content exactly as received before correction or transformation.
Error code
Stable label for a rejection reason, such as MISSING_OPERATOR.
Delivery ID
Unique name of a received message, different from the ID of the event it describes.
Correction
New delivery proposing to repair an earlier one with an explicit source.
Traceability
Ability to find where each value came from and what happened to it.
Recovery
Processing a corrected row after an earlier rejection.

Official references