Ir para o conteúdo

Lição 24 · Projects and Reference

Project: Add a Feature with Cursor

Build a CSV export for a small expense tracker with Cursor: set up rules, write a specification, plan, test first, build in steps, review and verify.

Time to combine everything. You will add a CSV export to a small expense tracker. The goal is not only the export but the whole workflow: configure, specify, plan, test, build, review and commit.

The code in this lesson is real Python that runs on this site. No agent replies are shown, because results differ from run to run. Your job is to compare what your agent produces with the checks below.

Step 1: prepare the project

Create a folder, run git init, and add tracker.py with this starting code:

Python
from dataclasses import dataclass
from datetime import date

@dataclass(frozen=True)
class Expense:
    day: date
    category: str
    note: str
    cents: int

Then add an AGENTS.md:

TEXT
# Expense tracker
- Python standard library only.
- Money is stored as integer cents. Never use floats for money.
- Tests: python -m unittest discover
- Ask before adding files that the task does not mention.

Add a .cursorignore that lists .env and exports/, and a .gitignore for the same paths and __pycache__/. Commit.

Step 2: write the specification

Decide the behavior before you ask for code:

Rule Decision
Columns date,category,note,amount
Date format ISO, such as 2026-12-01
Amount Two decimal places, made from cents without float math
Order By date, oldest first
Special characters Commas, quotes and line breaks must survive
Spreadsheet safety Text starting with =, +, -, @, a tab or a carriage return gets a leading apostrophe
Empty list Header row only

The spreadsheet rule matters. A note such as =HYPERLINK(...) may be treated as a formula when someone opens the file in a spreadsheet program. This is called CSV injection or formula injection. A leading apostrophe is a common defense, but it is not guaranteed in every program, so test with the programs your users use. It also changes the text, which matters if another program imports the file.

Step 3: plan

Switch to Plan mode and send:

TEXT
@tracker.py
Plan a function to_csv(expenses) -> str that follows this
specification: <paste the table>.
Include the test cases. No new dependencies, and no file writing yet.
Ask me if anything is unclear.

Check the plan. Does it use the csv module instead of joining strings? Does it avoid floats? Does it cover every row of your table?

Step 4: tests first

Ask the agent to write only the tests, or write them yourself. Read them before the implementation exists, and run them to see them fail.

Step 5: build and compare

Ask the agent to implement to_csv without changing the tests. Then compare with this reference solution. Yours may look different and still be correct, as long as it meets the specification.

Python Reference solution with acceptance checks

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that negative amounts are formatted by our own code from integers, so they get no apostrophe, while free text typed by users does.

Step 6: review

Use the passes from reviewing AI-written changes:

  • Did only the expected files change?
  • Is money handled without floats?
  • Were any tests weakened?
  • Do all imports come from the standard library?

Then, in a new chat, ask for a skeptical review that names a failing input for each finding. Try each input yourself.

Step 7: add the command

In a separate task, add a command that writes the export to a path the user gives. Decide first what happens when the file already exists. Refusing to overwrite is a safe default, and Python's exclusive mode "x" does that:

Python Never overwrite by accident

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Commit each task separately, with a message that explains why.

Stretch goals

  • Add a date range filter, planned and tested.
  • Write a skill that describes how to add a new export column.
  • Add a stop hook that sends one reminder to run the tests, with a loop limit.
  • Try the same task with two models, each in its own worktree, and compare.

Self-assessment

TEXT
[ ] I wrote the specification before the code
[ ] I saw the tests fail first
[ ] I can explain every line I kept
[ ] I tested commas, quotes, line breaks and formulas
[ ] Each commit is small and focused

If your result differs

Differences from the reference are not automatically mistakes. Judge them with evidence:

Difference Question to ask
Another sort order for equal dates Does the specification need a tie rule?
Windows line endings What do the users' programs expect?
An extra column or option Did anyone ask for it? If not, remove it
Decimal used for amounts Is it exact, and are cents still the stored form?
No apostrophe on risky text Which defense replaces it, and is it tested?

The first row shows a gap in the specification itself. Python's sorted keeps the original order of items with equal keys, so same-day expenses stay in input order. If users need something else, write the rule and add a test. Finding gaps like this is a normal and valuable part of the work. Update the specification first, then the tests, then the code.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Why use the csv module instead of joining strings with commas?

    CSV quoting rules are easy to get wrong by hand.
  2. 2. Why add an apostrophe to user text that starts with = in this export?

    It reduces formula injection risk. Test it with the programs your users use.
  3. 3. What does opening a file in mode x do if the file already exists?

    Exclusive creation fails instead of overwriting.

Summary

  • Set up instructions, ignore files and a commit before the work starts.
  • Specify behavior, plan, test first, then build.
  • Verify with acceptance checks and review in passes.
  • Split follow-up work into small tasks.

Next: Project: Fix a Bug with Cursor

Seu progresso fica salvo neste navegador. Não precisa de conta.