נבנה על ידיBuilt by Ariel Yohimovich

PythonPython
31
חלקPart 5: בונים דבריםBuilding things כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

פרויקט אמצעMidterm project

מחברים הכול לגרסה 1.0, עם תכנון, בדיקות והצגה.

Put it all together into version 1.0, with planning, tests and a demo.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • משפט אחד: מה הכלי עושהOne sentence: what the tool does
  • MVP: שלוש פקודותMVP: three commands
  • models.py: הנתוניםmodels.py: the data
  • storage.py: שמירה וטעינהstorage.py: saving and loading
  • argparse עם שלוש פקודותargparse with three commands
  • main(argv=None) לבדיקותmain(argv=None) for tests
  • tmp_path: תיקייה זמנית לכל בדיקהtmp_path: a temp folder per test
  • monkeypatch מחליף את נתיב הקובץmonkeypatch swaps the file path
  • מה, איך מתקינים, איך משתמשיםWhat, how to install, how to use

מונחיםTerms

  • הפרדת אחריותseparation of concerns: כל חלק בקוד אחראי על דבר אחד: מודל לנתונים, אחסון לשמירה, ממשק למשתמש. כך שינוי באחד לא שובר את השאר.Each part of the code is responsible for one thing: a model for data, storage for saving, an interface for the user. So a change in one doesn't break the rest.
  • READMEREADME: קובץ README.md בראש הפרויקט: מה זה, איך מתקינים, איך משתמשים. הדבר הראשון שאנשים רואים ב-GitHub.A README.md file at the top of a project: what it is, how to install, how to use. The first thing people see on GitHub.
  • מחווןrubric: רשימת קריטריונים עם ניקוד שמסבירה מראש מה נבדק בהגשה.A list of criteria with points that explains in advance what a submission is assessed on.
  • MVPMVP: Minimum Viable Product: הגרסה הקטנה ביותר שכבר עושה את העבודה. בונים אותה קודם, ומשפרים אחר כך.Minimum Viable Product: the smallest version that already does the job. Build it first, improve later.
  • monkeypatchmonkeypatch: fixture של pytest שמחליף זמנית משתנה או פונקציה בזמן בדיקה, למשל נתיב הקובץ.A pytest fixture that temporarily replaces a variable or function during a test, e.g. the file path.

Cheatsheet

תכנוןPlanning
PLAN = {
    "pitch": "Track the books I read, from the terminal",
    "mvp": ["add TITLE AUTHOR PAGES", "read TITLE PAGES", "stats"],
    "later": ["search", "tags", "charts", "API", "goals"],
}
print(PLAN["pitch"])
for i, cmd in enumerate(PLAN["mvp"], 1):
מבנהStructure
from dataclasses import dataclass, field, asdict


@dataclass
class Book:
    title: str
    author: str
ממשקInterface
import argparse
import sys
from pathlib import Path

from models import Book
from storage import load, save
בדיקותTests
from pathlib import Path

import cli
from models import Book
from storage import load, save

READMEREADME
README = """# Shelf

Track the books you read, from the terminal.

## Install

    python3 -m venv .venv
הגשהSubmit
RUBRIC = {"works": 30, "structure": 20, "tests": 20, "robust": 10, "readme": 10, "git": 10}

def score(checks):
    return sum(points for name, points in RUBRIC.items() if checks.get(name))

mine = {"works": True, "structure": True, "tests": True, "robust": False, "readme": True, "git": True}
print(score(mine), "/", sum(RUBRIC.values()))

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do
AssertionErrorמתחילים מכל הפיצ'רים ביחד, ואף אחד לא עובד עד הסוף.Starting with every feature at once, so none works fully.AssertionError: six features started, 0 finished: ...: מסיימים פקודה אחת עד הסוף (כולל בדיקה), ורק אז עוברים לבאה.AssertionError: six features started, 0 finished: ...: finish one command completely (tests included), then move on.
AssertionErrorשומרים את המדף לפני שבודקים שהפקודה הצליחה, ודורסים נתונים.Saving the shelf before checking the command succeeded, overwriting data.AssertionError: an error midway emptied the list, ...: שומרים רק בסוף, אחרי שכל השלבים הצליחו (ובכתיבה לקובץ זמני, כמו ב-storage.py).AssertionError: an error midway emptied the list, ...: save only at the end, after every step succeeded (and via a temp file, as in storage.py).
AssertionErrorREADME בלי הוראות התקנה.A README without install instructions.AssertionError: a stranger can't run the project: ...: מה שברור לכם לא ברור לאחרים. כותבים כל צעד, כולל venv.AssertionError: a stranger can't run the project: ...: what's obvious to you isn't to others. Write every step, venv included.
json.decoder.JSONDecodeErrorשוכחים שקובץ פגום מקריס את הכלי.Forgetting that a broken file crashes the tool.json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 20 (char 19): זה 10 נקודות במחוון. עוטפים את הטעינה ב-try (מפגש 21) ומציגים הודעה ברורה.json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 20 (char 19): that's 10 rubric points. Wrap loading in try (session 21) and show a clear message.

נבנה על ידיBuilt by Ariel Yohimovich