Project: course tracker

Combine data, classes, files and tests in a small tool you can extend.

Beginner 25 min read Lesson 16 of 480 Challenge included
What you will be able to do
  • Model a domain with a dataclass
  • Persist state as JSON
  • Structure a project for testing and growth

This project tracks completed lessons. It starts small, but the structure — model, storage, commands — is the same one larger tools use.

  1. 01
    Model the data

    A Tracker holds the learner name and a set of completed lesson slugs.

  2. 02
    Add behaviour

    Complete a lesson, compute a percentage and serialise the result.

  3. 03
    Persist

    Write the tracker to JSON and load it on the next run.

  4. 04
    Test

    Cover an empty tracker, a completed lesson and a full course.

tracker.pyPython
import json
from dataclasses import dataclass, field

@dataclass
class Tracker:
    learner: str
    completed: set[str] = field(default_factory=set)

    def complete(self, lesson: str) -> None:
        self.completed.add(lesson)

    def percent(self, total: int) -> float:
        return len(self.completed) / total * 100 if total else 0.0

    def to_json(self) -> str:
        return json.dumps(
            {"learner": self.learner, "completed": sorted(self.completed)}
        )

tracker = Tracker("Ada")
tracker.complete("getting-started")
tracker.complete("control-flow")
print(f"{tracker.percent(12):.0f}% complete")
print(tracker.to_json())
Your turn

Load, extend and save

Hard

Load the saved tracker, mark one more lesson complete and print the updated JSON.

PYTHON · starter
import json

saved = '{"learner": "Ada", "completed": ["control-flow"]}'
# your code here
Check your understanding before moving on.
Knowledge checkQuestion 1

Which function converts a Python dictionary into a JSON string?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 16 of 480 · 0% of course