Project

Capstone 1: The Data Model

Welcome to your capstone! The Guild Council asks you to build the Lantern Network: a task tracker that coordinates repairs across every island. You'll build it the way real apps are built, in layers:

  1. Model: what a task is (this lesson)
  2. Storage: saving and loading tasks as JSON
  3. Commands: turning typed text into actions
  4. The app: wiring it all together (the final boss!)

A validated dataclass

@dataclass writes __init__, __repr__ and __eq__ for you. __post_init__ runs right after __init__. It's the perfect place to clean up and validate data, so a bad task can never exist:

@dataclass
class Bridge:
    length: int

    def __post_init__(self):
        if self.length <= 0:
            raise ValueError("length must be positive")

dataclasses.asdict(obj) turns an instance into a plain dict, which is ready for JSON. cls(**data) builds one back from a dict.

Assignment

Complete Task:

  1. In __post_init__: strip spaces from title. Raise ValueError if the title is empty, or if priority is not 1, 2 or 3.
  2. to_dict() returns the task as a dict.
  3. The classmethod from_dict(data) builds a Task from such a dict.