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

PythonPython
20
חלקPart 3: קוד נקי ו-OOPClean code & OOP כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Git ו-GitHubGit & GitHub

commit, branch, merge, push ו-README טוב.

commit, branch, merge, push and a good README.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. מה הסדר הנכון?What's the right order?

2. איזה קובץ לא צריך להיכנס ל-Git?Which file shouldn't go into Git?

3. מה ההבדל בין Git ל-GitHub?What's the difference between Git and GitHub?

תרגיליםExercises

תרגילExercise 1

המאגר הראשון שלכםYour first repo

נבדק ידניתSelf-checked

על המחשב שלכם: התקינו Git, הגדירו שם ומייל, צרו תיקייה hello-git עם קובץ hello.py, ובצעו שני commits: אחד שיוצר את הקובץ ואחד שמשנה אותו. בסוף git log --oneline צריך להראות שתי שורות.

On your computer: install Git, set name and email, create a folder hello-git with hello.py, and make two commits: one creating the file and one changing it. At the end git log --oneline should show two lines.

רמזHint 1: git init → git add hello.py → git commit -m "Add hello".git init → git add hello.py → git commit -m "Add hello".
רמזHint 2: אחרי שינוי: git commit -am "..." מוסיף ושומר קבצים מוכרים.After a change: git commit -am "..." stages and saves known files.
לפתרוןSolution
תרגילExercise 2

מה ייכנס ל-Git?What goes into Git?

כתבו ignored(path, patterns) שמחזירה True אם הנתיב תואם לאחת התבניות. תבנית שמסתיימת ב-/ היא תיקייה (מתאימה אם הנתיב מתחיל בה או מכיל אותה), ושאר התבניות נבדקות מול שם הקובץ עם fnmatch. ואז to_commit(paths, patterns) מחזירה את מה שלא מתעלמים ממנו.

Write ignored(path, patterns) returning True if the path matches a pattern. A pattern ending in / is a folder (matches if the path starts with it or contains it); others are checked against the file name with fnmatch. Then to_commit(paths, patterns) returns the paths not ignored.

איך הפלט אמור להיראות?What should the output look like?
['shelf.py', 'README.md']
main.py
רמזHint 1: from fnmatch import fnmatch ו-path.split("/")[-1] לשם הקובץ.from fnmatch import fnmatch and path.split("/")[-1] for the name.
רמזHint 2: תיקייה: path.startswith(p) or "/" + p in "/" + path.Folder: path.startswith(p) or "/" + p in "/" + path.
לפתרוןSolution
תרגילExercise 3

לקרוא git logReading git log

פלט של git log --oneline נתון כמחרוזת. כתבו parse_log(text) שמחזירה רשימת זוגות (hash, message), ו-bad_messages(commits) שמחזירה hashes של הודעות קצרות מ-10 תווים או שהן אחת המילים fix, wip, stuff (בלי רגישות לאותיות).

git log --oneline output is given as a string. Write parse_log(text) returning a list of (hash, message) pairs, and bad_messages(commits) returning hashes of messages shorter than 10 characters or equal to fix, wip, stuff (case-insensitive).

איך הפלט אמור להיראות?What should the output look like?
5 ['9f8e7d6', '0d1e2f3', '77aa88b']
main.py
רמזHint 1: line.split(" ", 1) מחלק פעם אחת בלבד.line.split(" ", 1) splits only once.
רמזHint 2: דלגו על שורות ריקות.Skip blank lines.
לפתרוןSolution
תרגילExercise 4

בודק הודעות commitA commit message checker

כתבו check_message(msg) שמחזירה רשימת בעיות (רשימה ריקה = הודעה טובה): "empty" אם ריקה, "too long" אם השורה הראשונה מעל 50 תווים, "capital" אם לא מתחילה באות גדולה, "period" אם השורה הראשונה מסתיימת בנקודה.

Write check_message(msg) returning a list of problems (empty list = a good message): "empty" if empty, "too long" if the first line exceeds 50 characters, "capital" if it doesn't start with a capital letter, "period" if the first line ends with a period.

איך הפלט אמור להיראות?What should the output look like?
'Add search by author' []
'fixed stuff.' ['capital', 'period']
'' ['empty']
main.py
רמזHint 1: first = msg.strip().splitlines()[0] אחרי בדיקת ריק.first = msg.strip().splitlines()[0] after the empty check.
רמזHint 2: first[0].isupper()first[0].isupper()
לפתרוןSolution
תרגילExercise 5

מדף, צעד 20: הפרויקט ב-GitHubShelf, step 20: the project on GitHub

פרויקט מלווהCompanion project נבדק ידניתSelf-checked

על המחשב שלכם: בתיקיית פרויקט המדף צרו .gitignore (עם .venv/, __pycache__/, .env), git init, commit ראשון של כל הקבצים, צרו repo ב-GitHub, חברו עם git remote add origin ו-git push -u origin main. בדקו בדפדפן שהקבצים שם ושאין שם .venv.

On your computer: in the shelf project folder create .gitignore (with .venv/, __pycache__/, .env), git init, a first commit of all files, create a repo on GitHub, connect with git remote add origin and git push -u origin main. Check in the browser that the files are there and .venv isn't.

רמזHint 1: git add . מוסיף הכול חוץ ממה שב-.gitignore.git add . adds everything except what's in .gitignore.
רמזHint 2: ב-VS Code: Source Control → Publish to GitHub עושה את כל השלבים בלחיצה.In VS Code: Source Control → Publish to GitHub does all the steps in one click.
לפתרוןSolution

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

אתגרChallenge 1

מפענח git statusA git status decoder

git status --porcelain מחזיר שורות כמו M shelf.py, M app.py, ?? notes.txt, A new.py, D old.py. שני התווים הראשונים: מצב ב-staging ומצב בתיקייה. כתבו summarize(text) שמחזירה מילון עם הרשימות staged, modified, untracked (קובץ יכול להופיע גם ב-staged וגם ב-modified, למשל MM).

git status --porcelain returns lines like M shelf.py, M app.py, ?? notes.txt, A new.py, D old.py. The first two characters: staging state and folder state. Write summarize(text) returning a dict with lists staged, modified, untracked (a file can be both staged and modified, e.g. MM).

איך הפלט אמור להיראות?What should the output look like?
{'staged': ['shelf.py', 'both.py'], 'modified': ['app.py', 'both.py'], 'untracked': ['notes.txt']}
main.py
רמזHint 1: x, y, path = line[0], line[1], line[3:]x, y, path = line[0], line[1], line[3:]
רמזHint 2: ?? = untracked. אחרת: x שאינו רווח = staged, y שאינו רווח = modified.?? = untracked. Otherwise: non-space x = staged, non-space y = modified.
לפתרוןSolution
אתגרChallenge 2

diff משלכםYour own diff

כתבו mini_diff(old, new) שמקבלת שתי מחרוזות רב-שורתיות ומחזירה רק את שורות השינוי בפורמט של git: -שורה לשורה שהוסרה ו-+שורה לשורה שנוספה. השתמשו ב-difflib.ndiff ושמרו רק שורות שמתחילות ב-- או + .

Write mini_diff(old, new) taking two multi-line strings and returning only the change lines in git style: -line for removed and +line for added. Use difflib.ndiff and keep only lines starting with - or + .

איך הפלט אמור להיראות?What should the output look like?
-shelf = ["Dune", "Emma"]
+shelf = ["Dune", "Emma", "Matilda"]
-for b in shelf:
+for b in sorted(shelf):
main.py
רמזHint 1: difflib.ndiff(old.splitlines(), new.splitlines())difflib.ndiff(old.splitlines(), new.splitlines())
רמזHint 2: d[0] + d[2:] מוריד את הרווח.d[0] + d[2:] drops the space.
לפתרוןSolution

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