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

PythonPython
36
חלקPart 6: Python בעידן ה-AI וקריירהPython in the AI era & career כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

פרויקט גמר והצגהFinal project & demo

תכנון, בנייה, בדיקות, פרסום והצגה.

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

1. למה CLI ו-API קוראים לאותה שכבת שירות?Why do the CLI and API call the same service layer?

2. מה עושה CI?What does CI do?

3. הוספתם פיצ'ר בלי לשבור כלום בגרסה 2.3.1. מה הגרסה הבאה?You added a feature without breaking anything in 2.3.1. What's the next version?

תרגילים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)}
רמזHint 2: הפרה: rank[dep] >= rank[src].A violation: rank[dep] >= rank[src].
לפתרוןSolution
תרגילExercise 2

קריאת pyprojectReading pyproject

כתבו 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?
shelf 2.0.0 (python >=3.12): 2 deps, commands: shelf
main.py
רמזHint 1: p = tomllib.loads(text)["project"]p = tomllib.loads(text)["project"]
רמזHint 2: ", ".join(p.get("scripts", {})) or "none"", ".join(p.get("scripts", {})) or "none"
לפתרוןSolution
תרגילExercise 3

גרסה הבאהThe next version

כתבו 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)
רמזHint 2: major, minor, patch = map(int, version.split("."))major, minor, patch = map(int, version.split("."))
לפתרוןSolution
תרגילExercise 4

שירות עם חוקיםA service with rules

כתבו 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.
רמזHint 2: max(0, min(b["pages"], b["read"] + pages))max(0, min(b["pages"], b["read"] + pages))
לפתרוןSolution
תרגילExercise 5

מדף 2.0: פרויקט הגמרShelf 2.0: the final project

פרויקט מלווה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.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

מיגרציות מסדDatabase migrations

מסדי נתונים משתנים בין גרסאות. כתבו 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?
2 1 0
['id', 'title', 'pages', 'read'] 3
main.py
רמזHint 1: current = con.execute("PRAGMA user_version").fetchone()[0]current = con.execute("PRAGMA user_version").fetchone()[0]
רמזHint 2: con.execute(f"PRAGMA user_version = {i + 1}") (מספר שלנו, לא קלט משתמש).con.execute(f"PRAGMA user_version = {i + 1}") (our own number, not user input).
לפתרוןSolution
אתגרChallenge 2

בריאות הפרויקטProject health

כתבו 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.
רמזHint 2: re.search(r"sk-[A-Za-z0-9_-]{12,}", text)re.search(r"sk-[A-Za-z0-9_-]{12,}", text)
לפתרוןSolution

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