Python traps · Chapter 7 of 16
Shallow copy of nested data
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Everyday traps
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Python traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.py: one customer's swap in everyone's Monday
Write reproduce() in repro.py. Make plans for two customers with plan_for(customer_id) from plans.py (any ids you like), swap "dal-tadka" for "chole" on "mon" in the first plan only with swap_dish(plan, day, old, new), and return the second plan's Monday list, plan["week"]["mon"].
The check runs your reproduce() twice: beside the original plans.py it must return ['chole', 'roti'] (the first customer's swap turned up in the second plan), and beside a fixed plans.py it must return ['dal-tadka', 'roti']. So it must really make the two plans and swap the dish. No real waiting and no input(): reproduce() must finish within 3 seconds.
- Task 2
plans.py: every plan gets its own week
Fix plans.py so nothing is shared by accident. plan_for(customer_id) must give {"customer": customer_id, "week": ...} where the week has DEFAULT_WEEK's days and dishes but shares no list (and no dict) with DEFAULT_WEEK or with any other plan, so swap_dish on one plan never changes another plan or DEFAULT_WEEK, however many plans and swaps there are. slot_grid(days, slots) must give days rows of slots cells, every cell None and every row its own list, so setting one cell changes only that cell (slot_grid(0, 3) is []).
Keep DEFAULT_WEEK's days and dishes, day_line, and the line swap_dish prints (plan <customer>: <day> <old> swapped for <new>) as they are. The check imports a fresh copy of your plans.py and uses its own customers, days and grid sizes. copy.copy, dict(...) and .copy() are the same one-level copy as the bug.
- Task 3
test_plans.py: a test that would have caught it
Write a pytest file, test_plans.py, that would have caught this bug before it reached the kitchen. The check runs your file three times: beside the original plans.py, where at least one test must fail; beside the fixed plans.py, where every test must pass; and beside a wrong fix that makes each plan with copy.copy (still a one-level copy) and fixes the grid, where at least one test must fail.
So change a dish inside one plan's week and check another plan, and DEFAULT_WEEK, still have the old dish; checking only the plan you changed passes every version. Test the grid too: set one cell and check the other rows are still empty. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.py: one customer's swap in everyone's Monday
- plans.py: every plan gets its own week
- test_plans.py: a test that would have caught it
More in Everyday traps
Build "Shallow copy of nested data" yourself
DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. 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 are free. This one comes with the all labs plan.