Open lesson

Mission: preserve the awkward page

Command palette

This mission workspace keeps code and notes in this browser tab. Export them before closing it. Running code is practice, not an automatic grade for the mission.

Brief

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

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 mission

  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 check your work

  • 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.

Course example

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)

Code

Use Tab to leave the editor. Ctrl+Enter runs the source. The lines beginning with # are task reminders. Python ignores them. Write your code beneath them.

Python console

Loading laboratory…

Explore Python or SQL here; this is not an operating-system shell.

This is a disposable browser experiment. The code run here is not recorded as a completed mission.

Each command starts a fresh disposable worker: it runs a captured copy of your current source, then your command with the lesson fixtures. Changes are discarded afterward. Commands never save source or award XP. Ctrl+Enter executes the command.

Virtual workspace files

Source and declarative assets are readable here without JavaScript. Python input files are created at runtime; SQL setup initializes the disposable database.

source.py

Current editor source. Inspection shows your current edits.

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

exemple.py

Declarative lesson asset; this is not a file created in the Python filesystem.

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)