Python traps · Chapter 14 of 16

A cache that never forgets

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

  • Part: Time
  • 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.

  1. Task 1

    repro.py: a hundred quotes that will not go away

    Write reproduce() in repro.py. Make 100 FareQuote objects from quotes.py, one per order (any ids, any areas from RATES), ask each one for a fee with fee(km), and keep a weakref to each. Then drop every quote (delete the list and the loop variable), run gc.collect(), and return how many of the 100 weakrefs still give an object, as an int.

    The check runs your reproduce() twice, each time in a fresh python3: beside the original quotes.py it must return 100 (every dropped quote still alive), and beside a fixed quotes.py it must return 0. So it must really make the quotes, ask the fees and drop them: count, never measure memory. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    quotes.py: a quote is freed when nobody needs it

    Fix FareQuote in quotes.py. fee(km) still gives the area's base fee plus the fee per km times km, from rates(area) (FareQuote({"id": "D2041", "area": "Powai"}).fee(4) is 54). Asking the same quote for the same distance again must come from a cache: rates is called once per quote and distance. A quote that nobody holds any more must be freed, with its cached fees, so the cache never grows past the quotes still in use.

    Keep FareQuote(order, rates=lookup_rate), fee(km), RATES and lookup_rate as they are. The check imports a fresh copy of your quotes.py, passes its own counting rates function and its own areas, makes hundreds of quotes, drops them and uses weakrefs after gc.collect() to see which were freed. A smaller maxsize is not a fix: it still keeps up to that many quotes alive.

  3. Task 3

    test_quotes.py: a test that would have caught it

    Write a pytest file, test_quotes.py, that would have caught this leak before it reached production. The check runs your file three times: beside the original quotes.py, where at least one test must fail; beside the fixed quotes.py, where every test must pass; and beside a wrong fix that sets maxsize=128, where at least one test must fail.

    So test both sides: a quote that is dropped is freed (keep a weakref, delete the quote, gc.collect(), and check the weakref gives None; one quote is enough, since a bounded cache still keeps it), and asking the same fee twice calls rates only once (pass your own rates function that counts its calls). No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: a hundred quotes that will not go away
  • quotes.py: a quote is freed when nobody needs it
  • test_quotes.py: a test that would have caught it

More in Time

Build "A cache that never forgets" 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.