
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:
- Model: what a task is (this lesson)
- Storage: saving and loading tasks as JSON
- Commands: turning typed text into actions
- 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:
- In
__post_init__: strip spaces fromtitle. RaiseValueErrorif the title is empty, or ifpriorityis not 1, 2 or 3. to_dict()returns the task as a dict.- The classmethod
from_dict(data)builds aTaskfrom such a dict.
Complete Task:
- In
__post_init__: strip spaces fromtitle. RaiseValueErrorif the title is empty, or ifpriorityis not 1, 2 or 3. to_dict()returns the task as a dict.- The classmethod
from_dict(data)builds aTaskfrom such a dict.
main.py Python sleeping
Press Run to try your code, then Check to test it.