Introduction
A to-do list is the classic CRUD (create, read, update, delete) application. This version adds priorities, due dates relative to a configurable 'today', smart sorting and statistics.
Problem statement
Objectives
- Build a command loop with dispatch on the first word
- Use datetime.date for due-date arithmetic
- Sort with a tuple key
- Keep task numbers stable after deletions
Features
- High, normal and low priorities
- Due dates with overdue, today and days-left labels
- Sorting by priority, then due date, then number
- done / undo / remove / clear
- Statistics with completion rate
Concepts demonstrated
- CRUD operations
- Date arithmetic
- Sorting with tuple keys
- Exceptions for invalid input
System architecture
Task is a dataclass with a describe method that builds the display line relative to today. TodoList.add parses the words of an add command, pulling out priority and due-date options and keeping the rest as the title. pending_sorted sorts by a (priority, due date, number) tuple, using date.max for tasks without a due date. run dispatches a command; main handles errors and the loop.
Folder structure
Complete source code
main.py
What each file does
- main.py
- The whole program: data classes, validation, the command loop and the report.
How to run it
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
today 2026-11-27 add Submit lab record !high due 2026-11-26 add Buy groceries add Revise Python OOP !high due 2026-11-30 add Call the bank due 2026-11-27 add Clean the room !low add !high add Pay fees due 27-11-2026 list done 2 done 4 done 9 list all undo 4 clear stats remove 5 list quit
Testing
- Run
python main.py < tests/sample-input.txtand compare withtests/expected-output.txt. - With today set to 27 November 2026, the lab record is overdue by 1 day and the bank call is due today.
- A task with no title and a date in the wrong format are rejected.
- After undo 4 and clear, stats report 4 tasks with 1 overdue.
Common errors
Sorting fails when some tasks have no due date
Use t.due or date.max so missing dates sort last instead of comparing None with a date.
Task numbers change after deleting
Give each task a permanent number from a counter rather than using list positions.
Possible improvements
- Save tasks to a JSON file between runs
- Add tags and filter by tag
- Add recurring tasks such as 'every Monday'
Additional challenges
- 1Add an 'edit <n> <new title>' command.
- 2Show tasks grouped under Today, This week and Later headings.
- 3Add an 'agenda' command that lists the next 7 days.
Built and tested it yourself? Mark it to update your dashboard.