──────────────────────────────────────

20. Repair Desk Intake · Build a Repair Desk Intake Report

Build a streamed report with accepted counts and rows that need review.

Lesson files · lessons/iterators-and-toolkit/repair-desk-report

Before you start
- Use yield to pass cleaned values from a loop.
- Use Counter to total and rank categories.
- Split a record into fields and check its shape.

By the end
- Process a multi-line intake export one row at a time and tally the valid rows.
- Flag malformed rows while still reporting later valid rows.
- Demonstrate deterministic output for mixed, empty and all-invalid input.

Where you can use it
A repair coordinator can plan staffing by category and see how many slips a volunteer needs to review before the event.

Keep row validation inside a generator and reporting outside it. In the trial export below, ? marks a damaged entry. The generator yields None for that entry, yields a category for each accepted entry, and skips blank lines. In your report, check both fields of each intake row before yielding its result.

Counter can consume the generator directly. Its pop(None, 0) call removes and returns the review count, with a default of 0 when every row was accepted. Now total() counts only the accepted requests, and most_common() lists the categories with their counts. The tally stores one count per category and one for review, even if thousands of rows arrive.

Predict
What number prints after “Accepted: ”?
- 4
- 3

Worked example (example.py)

from collections import Counter


def preview_categories(lines):
    for raw in lines:
        category = raw.strip()
        if category == "?":
            yield None
        elif category:
            yield category


counts = Counter(preview_categories(["bike", "", "?", "sewing", "bike"]))
review = counts.pop(None, 0)
print(f"Accepted: {counts.total()}")
print(f"Needs review: {review}")
for category, count in counts.most_common():
    print(f"{category}: {count}")


Example result
Accepted: 3
Needs review: 1
bike: 2
sewing: 1

Use a generator to read ticket|category rows such as R04|sewing. Skip blank lines and lines that start with # after trimming. Each valid row has exactly two fields, a nonempty ticket and a category of bike, battery or sewing. Trim both fields and lowercase the category. Count each valid row and flag each invalid row for review. Print “Accepted: N”, “Needs review: N”, then one “category: N” line per accepted category in most_common() order.

Starter program (starter.py)

import sys
from collections import Counter

ALLOWED_CATEGORIES = {"bike", "battery", "sewing"}


def read_categories(lines):
    for raw in lines:
        row = raw.strip()
        if row:
            yield row


counts = Counter(read_categories(sys.stdin))
print(f"Accepted: {counts.total()}")
print("Needs review: 0")


Starter input
 # Morning intake
R04|sewing
broken slip
 R05 | BIKE 

R06|bike
R08|bike|sewing
R07|Battery

Hint
Split on | and check the field count before unpacking. Yield a valid category or None for review. Remove the review key from the tally before printing its total.

Case 1. Try the sample
Input
 # Morning intake
R04|sewing
broken slip
 R05 | BIKE 

R06|bike
R08|bike|sewing
R07|Battery
Expected output
Accepted: 4
Needs review: 2
bike: 2
sewing: 1
battery: 1

Case 2. Closed desk
Input
(No input.)
Expected output
Accepted: 0
Needs review: 0

Case 3. Every ticket needs review
Input
|bike
R09|  
R10|glass
R11|battery|
Expected output
Accepted: 0
Needs review: 4

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Try a decorator in a separate file. Write with_heading(report) so it returns a nested run() that prints “Repair desk” and returns report(). Write show_totals() to print “Accepted: 2”. Put @with_heading above its def line, then call show_totals(). Python applies with_heading(show_totals) when it defines the function. Check that the heading prints on each call.

Reflect
How would you check that each nonblank, noncomment row was counted exactly once?

Check your prediction
3 The blank line yields nothing. The damaged entry adds to the review count, which comes out of the tally before total().
