Lesson 30 of 30

In progress

Course progress

Python

Capstone: a task manager that remembers

Build a small task manager with JSON storage, command handling and validation, then extend it without rewriting it.

This is the last lesson in the track, and it uses everything: functions, lists of dictionaries, JSON, error handling and a command loop. The goal is not a product. It is a program you can explain line by line.

What you will learn

  • Model tasks as a list of dictionaries with ids derived from the data.
  • Separate commands, lookup and rendering so each can be tested alone.
  • Recover from bad input at the command boundary instead of crashing.
  • Persist the list to JSON without risking a half-written file.

What you will build

A task manager with four commands:

  • add <title> creates a task with a unique id.
  • list shows every task and whether it is done.
  • done <id> marks one task complete.
  • remove <id> deletes a task.

Tasks are a list of dictionaries. JSON stores them. Invalid ids and empty titles are refused with a message rather than a traceback.

Start with the data shape

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

next_id uses max with a default of zero, so the first task gets id 1 and no counter has to be stored separately. Deriving the id from the data means a hand-edited file cannot leave the counter out of sync.

Commands as functions

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

find is the only place that knows how lookup works, so complete and remove cannot disagree about what "not found" means. render returns a string instead of printing, which keeps it usable in a test.

A command loop that survives mistakes

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

int(argument) can raise ValueError for "done nine", and find raises it for a missing id; one handler covers both and prints a sentence instead of a traceback. That is the difference between a script and a tool.

Persistence

The runnable version above keeps tasks in memory because this page cannot rely on writable disk. On your machine, add two functions around the same list.

import json
from pathlib import Path
DATA = Path(__file__).parent / "tasks.json"
def load():
try:
return json.loads(DATA.read_text(encoding="utf-8"))
except FileNotFoundError:
return []
except json.JSONDecodeError:
print("tasks.json is damaged; starting with an empty list")
return []
def save(tasks):
temporary = DATA.with_suffix(".tmp")
temporary.write_text(json.dumps(tasks, indent=2, ensure_ascii=False), encoding="utf-8")
temporary.replace(DATA)

Load once at startup, save after every command that changes the list. Writing to a temporary file and replacing avoids leaving a half-written JSON file if the process stops mid-save. The FileNotFoundError branch makes the first run work; the JSONDecodeError branch keeps a damaged file from ending the program.

Call save(tasks) inside add, complete and remove, or in the loop after a successful command. Saving in one place after success is simpler and avoids writing on failed commands.

Extend it without rewriting it

The structure pays off when requirements change:

  • Priorities: add "priority": "normal" in new_task and a --priority argument; render sorts by it.
  • Due dates: store ISO strings, compare them as strings, and format for display.
  • Search: filter tasks by a substring, then reuse render.
  • Multiple lists: turn tasks into a dictionary keyed by list name, and pass the chosen list into the same functions.

Each extension touches one function because each function has one job. That is the point of the whole track.

Forgetting to save after a successful change is the classic capstone bug: the command reports success, the list looks right, and the data vanishes on restart. Test it deliberately: add a task, exit, start again, list. If the file is empty, the save call is in the wrong branch or the path points somewhere unexpected.

Add a --all style filter to render: given a show argument of "open", print only unfinished tasks. Then make missing count how many tasks are unfinished and print it after the list.

Show solution
Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Filtering happens before rendering, and the open count is computed from the full list so it stays correct when the filter hides completed tasks.

Where to go next

You have finished the Python track. The next useful steps are testing with unittest or pytest, working with an API using urllib.request, and packaging a script so it can be installed. All three build directly on modules, JSON and error handling.

Recap

  • Model tasks as dictionaries, derive ids from the data, and keep lookup in one function.
  • Handle ValueError at the command boundary so bad input produces a sentence.
  • Save through a temporary file, and test persistence by restarting.

Why not use a database?
A JSON file is honest for a single-user tool with hundreds of tasks. A database earns its place with concurrent writers, queries across large data, or partial updates. Start with the file; migrate when the file is the problem.
How would I add tests to this project?
Import the functions, build a small list of tasks, and check return values instead of printed text. `assert complete(tasks, 1)[“done”] is True` is enough to start, and it runs without touching the real data file.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.