Python traps 2 · Chapter 4 of 16

strip takes characters, not a word

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.

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: the dishes sheet that lost its s

    Write reproduce() in repro.py. Ask table_for from imports.py which table the new dishes sheet, "dishes.csv", goes into, and return what it gives. If it raises ValueError, catch it and return the error's message, str(error), instead.

    The check runs your reproduce() twice: beside the original imports.py it must return "unknown table 'dishe'", and beside a fixed imports.py it must return "dishes". So it must really call table_for with "dishes.csv": a repro that just returns the message fails the second run. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    imports.py: every sheet finds its table

    Fix table_for in imports.py. A file named <table>.csv or upload_<table>.csv gives <table>, for every table in TABLES, whatever letters it starts or ends with: only the exact ".csv" at the end and the exact "upload_" at the start come off, once. A file name that does not end in ".csv" raises ValueError("not a csv file: <filename>"), and a table that is not in TABLES raises ValueError("unknown table '<table>'").

    Keep TABLES as it is, and keep plan_imports and its log lines, import: <file> -> <table> and import: skipped <file>: <error>. The check imports a fresh copy of your imports.py and adds tables of its own to TABLES, trying each with and without upload_: a fix that mends the end of the name but not the start is as wrong as the bug.

  3. Task 3

    test_imports.py: a test that would have caught it

    Write a pytest file, test_imports.py, that would have caught this bug before last night's import. The check runs your file three times: beside the original imports.py, where at least one test must fail; beside the fixed imports.py, where every test must pass; and beside a wrong fix that mends the .csv end but keeps lstrip("upload_"), where at least one test must fail.

    So test more than menu.csv: a table whose name ends in a letter of ".csv", such as dishes.csv, and an app upload whose table starts with a letter of "upload_", such as upload_orders.csv. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: the dishes sheet that lost its s
  • imports.py: every sheet finds its table
  • test_imports.py: a test that would have caught it

More in Names

Build "strip takes characters, not a word" 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.