Python traps 2 · Chapter 7 of 16

JSON turns int keys into strings

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Names
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.

Why you need this

back[7] would have raised KeyError: 7. But code that counts things usually asks .get(key, 0), and a missing key with a default of 0 is a perfectly good answer: "nothing booked". So the data is there, the code runs, and every number is wrong. Worse, a dict may hold both 2 and "2". After the restart the buggy service booked 50 more under the int key, and that night's file has the key twice:

{"1": 31, "2": 48, "3": 12, "2": 50}

The round trip changes other things too: a tuple comes back as a list, and sets, Decimal and dates do not save at all (TypeError: Object of type set is not JSON serializable). The cure is to convert at the edge, where the data comes in, so the rest of the code sees the types it expects:

back = json.loads('{"7": 340, "12": 95}')
wallets = {int(rider): rupees for rider, rupees in back.items()}
print(wallets, wallets.get(7, 0))
{7: 340, 12: 95} 340

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: 48 bookings lost in a restart

    Write reproduce() in repro.py. Start with an empty state, {}, and book 48 tiffins in slot 2 (1 pm) with book from slots.py. Save the state to a file inside a tempfile.TemporaryDirectory(), as the nightly job does, load it back with load, and return seats_left of the loaded state for slot 2, as an int.

    The check runs your reproduce() twice: beside the original slots.py it must return 50 (every booking lost), and beside a fixed slots.py it must return 2 (50 seats, 48 booked). So it must really book, save and load. Never write in the case's folder. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    slots.py: the slots remember their bookings

    Fix slots.py so bookings survive the nightly save and load. A state loaded from a file must have int slot ids as its keys, as before the save, so seats_left and book see every booking, and booking after a load works (book still raises ValueError when a slot has too few seats). A state that was never saved must keep working as it does today. load of a file that does not exist gives {}, and a file the old save wrote (such as {"1": 31, "2": 48}) must still load, as {1: 31, 2: 48}.

    Keep CAPACITY, SLOTS, book, seats_left, save and report as they are. The check imports a fresh copy of your slots.py and uses its own slots and files in a temporary folder: a fix that makes a loaded state work but breaks a state that was never saved is as wrong as the bug.

  3. Task 3

    test_slots.py: a test that would have caught it

    Write a pytest file, test_slots.py, that would have caught this bug before the restart. The check runs your file three times: beside the original slots.py, where at least one test must fail; beside the fixed slots.py, where every test must pass; and beside a wrong fix that makes seats_left look the slot up as a string, where at least one test must fail.

    So test both sides: bookings survive a save to a file and a load (use pytest's tmp_path folder), and bookings on a state that was never saved count too. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: 48 bookings lost in a restart
  • slots.py: the slots remember their bookings
  • test_slots.py: a test that would have caught it

More in Names

Build "JSON turns int keys into strings" yourself

Monsoon season at DabbaDrop brings a new pile of tickets from Kunal. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.