Planning, building, testing, publishing and presenting.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
בודק שכבותA layer checker
כתבו layer_violations(imports, order) שמקבלת מילון קובץ → רשימת קבצים שהוא מייבא, ורשימת שכבות מהנמוכה לגבוהה (["models", "storage", "service", "cli"]). קובץ מותר לייבא רק שכבה נמוכה ממנו. מחזירה רשימה ממוינת של זוגות (from, to) שמפרים את הכלל.
Write layer_violations(imports, order) taking a dict file → list of files it imports, and a list of layers from lowest to highest (["models", "storage", "service", "cli"]). A file may only import a lower layer. Return a sorted list of (from, to) pairs breaking the rule.
איך הפלט אמור להיראות?What should the output look like?
[('service', 'cli')]
main.py
רמזHint 1:rank = {name: i for i, name in enumerate(order)}rank = {name: i for i, name in enumerate(order)}
כתבו summary(text) שמקבלת תוכן של pyproject.toml ומחזירה מחרוזת name version (python REQ): N deps, commands: a, b, בעזרת tomllib. אם אין scripts, commands: none.
Write summary(text) taking a pyproject.toml's content and returning name version (python REQ): N deps, commands: a, b, using tomllib. With no scripts, commands: none.
איך הפלט אמור להיראות?What should the output look like?
כתבו next_version(version, commits) שמקבלת גרסה ורשימת הודעות commit, ומחזירה את הגרסה הבאה: אם הודעה כלשהי מתחילה ב-BREAKING → MAJOR; אחרת אם מתחילה ב-feat → MINOR; אחרת אם יש הודעות → PATCH; בלי הודעות, אותה גרסה.
Write next_version(version, commits) taking a version and commit messages, returning the next version: if any message starts with BREAKING → MAJOR; else if any starts with feat → MINOR; else if there are messages → PATCH; with none, the same version.
איך הפלט אמור להיראות?What should the output look like?
1.5.0 2.0.0 1.4.2
main.py
רמזHint 1:any(c.startswith("BREAKING") for c in commits)any(c.startswith("BREAKING") for c in commits)
כתבו Service שמקבל אובייקט אחסון (עם add(title, pages) -> id, get(title) -> dict | None, set_read(id, read)) ומממש: add (ValueError לכותרת ריקה או עמודים ≤ 0), read(title, pages) (LookupError לספר חסר, לא עוברים את pages ולא יורדים מתחת ל-0, מחזיר אחוז), ו-stats() (מספר ספרים ואחוז כולל, דרך storage.all()). אחסון בזיכרון נתון.
Write a Service taking a storage object (with add(title, pages) -> id, get(title) -> dict | None, set_read(id, read)) implementing: add (ValueError on an empty title or pages ≤ 0), read(title, pages) (LookupError for a missing book, never past pages or below 0, returns a percent), and stats() (book count and overall percent, via storage.all()). An in-memory storage is given.
איך הפלט אמור להיראות?What should the output look like?
75 100 0 (2, 80)
main.py
רמזHint 1:החוקים ב-Service, לא באחסון.Rules in the Service, not in storage.
פרויקט מלווהCompanion projectנבדק ידניתSelf-checked
על המחשב שלכם: בנו את מדף 2.0 כחבילה shelf/ (models, storage עם SQLite, service, cli, api), pyproject.toml, בדיקות ל-service ול-API (TestClient), README עם צילום מסך של /docs, ו-.github/workflows/tests.yml. הוסיפו תוספת אחת לבחירתכם (גרף, המלצה, ייבוא ISBN או גיבוי). העלו ל-GitHub עם CI ירוק ותגית v2.0.0.
On your computer: build Shelf 2.0 as a shelf/ package (models, SQLite storage, service, cli, api), pyproject.toml, tests for the service and the API (TestClient), a README with a screenshot of /docs, and .github/workflows/tests.yml. Add one extra of your choice (a chart, recommendations, ISBN import or a backup). Push to GitHub with green CI and a v2.0.0 tag.
רמזHint 1:בונים מלמטה למעלה: models → storage → service, עם בדיקות לכל שכבה לפני הבאה.Build bottom up: models → storage → service, with tests for each layer before the next.
רמזHint 2:pip install -e ".[dev]" ואז shelf --help אמור לעבוד בטרמינל.pip install -e ".[dev]" then shelf --help should work in the terminal.
מסדי נתונים משתנים בין גרסאות. כתבו migrate(con, migrations): migrations היא רשימת מחרוזות SQL, והגרסה הנוכחית שמורה ב-PRAGMA user_version. הפונקציה מריצה רק את המיגרציות שעוד לא רצו (לפי אינדקס), כל אחת בטרנזקציה, מעדכנת את user_version ומחזירה את מספר המיגרציות שהורצו.
Databases change between versions. Write migrate(con, migrations): migrations is a list of SQL strings, and the current version is stored in PRAGMA user_version. The function runs only migrations that haven't run yet (by index), each in a transaction, updates user_version and returns how many ran.
איך הפלט אמור להיראות?What should the output look like?
כתבו health(files) שמקבלת מילון נתיב → תוכן של פרויקט, ומחזירה ציון 0–100 ורשימת המלצות: README.md עם ## Install (20), לפחות 5 פונקציות def test_ בקבצי test_*.py (25), .gitignore שמכיל .venv (10), workflow ב-.github/workflows/ שמריץ pytest (20), pyproject.toml עם version (10), ואף קובץ .py לא מכיל מחרוזת שנראית כמו מפתח sk- (15).
Write health(files) taking a dict path → content of a project, returning a 0–100 score and a list of recommendations: README.md with ## Install (20), at least 5 def test_ functions in test_*.py files (25), a .gitignore containing .venv (10), a workflow in .github/workflows/ running pytest (20), pyproject.toml with version (10), and no .py file containing a string that looks like an sk- key (15).
איך הפלט אמור להיראות?What should the output look like?
40
- write more tests (3/5)
- add a CI workflow that runs pytest
- remove the API key from the code
main.py
רמזHint 1:בונים רשימת (points, ok, advice) ואז סוכמים.Build a list of (points, ok, advice) then sum.