Python traps 2 · Chapter 8 of 16

Two ways to write é

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

Every comparison is still a correct comparison: those strings really are different rows of code points. .lower() does not help, since it changes case, not form. And "contains" fails quietly: "crème" is not inside "crème caramel", so the search simply finds nothing, and plain names work, so every test with "Rohan Desai" passed.

The cure is to bring both sides to one form before you compare. unicodedata.normalize("NFC", text) turns either spelling into the composed one, and .casefold() is the stronger .lower() made for comparing ("Straße".casefold() is 'strasse', .lower() keeps the ß):

def same(a, b):
    return unicodedata.normalize("NFC", a).casefold() == unicodedata.normalize("NFC", b).casefold()


print(same(typed, from_app), same("CRÈME", from_app))
True True

Normalize a copy for the comparison; keep the text as it was saved for showing it.

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: José, there and not found

    Write reproduce() in repro.py. Make a list of one customer, the dict {"id": "C064", "name": ..., "area": "Bandra"}, whose name is "José Pereira" written the way the iPhone app sends it: unicodedata.normalize("NFD", "José Pereira"). Search it with find_customers from support.py for "josé", typed as a plain string in your code (as the desk types it), and return the list of ids it gave.

    The check runs your reproduce() twice: beside the original support.py it must return [] (José not found), and beside a fixed support.py it must return ["C064"]. So it must really search a decomposed name. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    support.py: the desk finds every customer

    Fix find_customers in support.py so it finds a name however its accents were saved. A name saved decomposed (NFD, as the iPhone app sends it) must be found by a composed (NFC) query, as the desk types it, and a composed name by a decomposed query. Case does not matter ("JOSÉ" finds José), names without accents work as today, spaces around the query are ignored, and no match gives []. It is still a "contains" search on the name, and it gives the ids in the order of the customers list.

    Keep display_name as it is, and never change the customers' names: the desk shows them as they were saved. The check imports a fresh copy of your support.py and uses its own customers and queries, with accents saved both ways, on both sides: a fix that changes only the query is as wrong as the bug.

  3. Task 3

    test_support.py: a test that would have caught it

    Write a pytest file, test_support.py, that would have caught this bug before the iPhone app's customers reached the desk. The check runs your file three times: beside the original support.py, where at least one test must fail; beside the fixed support.py, where every test must pass; and beside a wrong fix that normalizes only the query, where at least one test must fail.

    So test the ticket's side: a name saved decomposed (built with unicodedata.normalize("NFD", ...)) and searched with a composed query, as you type it. Add the other way round, case, and a plain name. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: José, there and not found
  • support.py: the desk finds every customer
  • test_support.py: a test that would have caught it

More in Names

Build "Two ways to write é" 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.