[ norboten ]
python · lab python-03

The Report That Crashes on One Name

· about 35 minutes · runs on ubuntu-26.04-devops · unrated

An unrated lab. It runs on your machine with no account and no network, and everything about it — the faults, the checks, the hints and the reference solution — is in the repository. An attempt on it is recorded on your profile and never moves a rating: only rated labs do. Rated and unrated labs.

The briefing

/opt/members/report.py reads the membership exports in /srv/members/ and writes a summary of the members per city to /var/lib/members/summary.csv. members-report.timer runs it shortly after boot and every night.

Two systems export into that directory. The new one writes UTF-8. The old one, a Windows tool nobody maintains, writes cp1252 — its files are named *.cp1252.csv, and that is documented in /srv/members/README. Since a member called Ruiz Peña joined, the job has failed every night with a UnicodeDecodeError, and the summary is a week old. A colleague "fixed" it once by ignoring the characters that would not decode, and the report then listed Pea as a member's name.

What is expected, and graded — the grader runs report.py itself, through the MEMBERS_DIR and SUMMARY_OUT variables the script already reads:

  1. Every export is read: a *.cp1252.csv file as cp1252, every other *.csv as UTF-8.
  2. Every name reaches the summary exactly as it was written, with no character dropped or replaced.
  3. The summary is written as UTF-8, whatever the machine's locale, and is valid CSV.
  4. The run started by members-report.timer succeeds and the summary holds every city — and still does after a reboot.

You have root through sudo.

What is graded

The machine is checked, rebooted, and checked again. A check passes only if it passes both times.

CheckObjective
01_the_old_export_is_readRead and write text files with the encoding stated, not the machine's
02_every_name_survivesKeep every character of the data, instead of dropping what does not decode
03_the_output_is_utf8Read and write text files with the encoding stated, not the machine's
04_the_nightly_run_succeedsRun a job from a systemd timer and prove its output is complete

Start it

  1. 2Labs
  2. python-03select this one
  3. udownload it
  4. sstart the machine

Reading

Where the lab's hints send you, level by level, as you ask for them (h, then l opens a journal section in the TUI).

Theory for this lab

4 questions on the same topic, in the lab's Theory tab. They never affect the lab's grade. Three of them, to answer here:

Question 1

What does this print?

data = 'Peña'.encode('cp1252')
print(data.decode('utf-8', errors='ignore'))
Question 2

What does this print?

data = 'München'.encode('utf-8')
print(data.decode('latin-1'))
Question 3

A Python program calls `open("/srv/export.csv")` with no `encoding=`. Which encoding is used to decode the file?