Project: course tracker
Combine data, classes, files and tests in a small tool you can extend.
- 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.
- 01Model the data
A Tracker holds the learner name and a set of completed lesson slugs.
- 02Add behaviour
Complete a lesson, compute a percentage and serialise the result.
- 03Persist
Write the tracker to JSON and load it on the next run.
- 04Test
Cover an empty tracker, a completed lesson and a full course.
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())Load, extend and save
Load the saved tracker, mark one more lesson complete and print the updated JSON.
import json
saved = '{"learner": "Ada", "completed": ["control-flow"]}'
# your code hereWhich function converts a Python dictionary into a JSON string?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.