Capstone project
Capstone: a command-driven task tracker
Lesson 20 of 20 · 28 min · Module 6: Bigger programs
Build the engine of a small task tracker that understands typed commands, like a command-line tool or a chat bot. It brings together everything from this course.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Design a small command interpreter
- Use exceptions for control of error paths
- Combine dataclasses, parsing, and sorting in one program
The idea
Each command is one line of text. The engine keeps its state between commands and returns one output line per command. Invalid commands must not crash the program: they produce an error: … line and the tracker keeps working.
commands = ["add Buy milk !2", "add Write report", "list"]
for command in commands:
print(command)
01One loop, one output per command
The engine reads a command, changes its state, and appends exactly one line of output. That simple contract makes the program easy to test: a list of commands in, a list of lines out.
02Raise once, catch once
Each problem raises CommandError with its message. A single except at the bottom of the loop turns it into an error: … line. Without this pattern, the error handling would be copied into every branch.
class CommandError(Exception):
pass
try:
raise CommandError("missing title")
except CommandError as err:
print(f"error: {err}")error: missing title03Separate parsing from acting
First split the line into command and arguments and validate them, then change the state. Keeping these steps apart makes it easy to add new commands, such as edit or undo.
Your project brief
run_commands(lines) that processes commands in order and returns a list of output lines:•
add TITLE or add TITLE !N (priority N from 1 to 5, default 3) → added #ID, IDs start at 1;•
done ID → completed #ID;•
delete ID → deleted #ID;•
list → #ID [!P] TITLE for each open task, sorted by priority then ID, joined with "; " in a single output line, or no open tasks;•
stats → T tasks, D done (deleted tasks are not counted).Errors produce
error: unknown command, error: missing title, error: invalid priority, or error: no task #ID (also for an ID that is not a number, shown as written).What the checks look for
- Adds tasks and lists them by priority, then ID
- Completes and deletes tasks and reports stats
- IDs are never reused after a delete
- Reports errors and keeps working
- Handles an empty list and titles that contain an exclamation mark
Take this with you
Real programs are small, well-defined pieces combined carefully: parse, validate, act, report.
Go a little further
Add an undo command that reverses the last add, done, or delete.