-
-
Notifications
You must be signed in to change notification settings - Fork 92
London | 26-SDC-Mar | Ebrahim Beiati-Asl | Sprint 5 |Sprint 5 prep #433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ebrahimbeiati
wants to merge
10
commits into
CodeYourFuture:main
Choose a base branch
from
ebrahimbeiati:sprint-5-prep
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cc96971
why we use types task
ebrahimbeiati 732a40b
Type checking with mypy
ebrahimbeiati e1f9545
classes and objects task
ebrahimbeiati 405f2c4
methods task
ebrahimbeiati 49fae75
add dataclasses exercise
ebrahimbeiati 625be12
generics task
ebrahimbeiati 527becb
Type guided refactorings task
ebrahimbeiati 3a5ecd1
enum exercise
ebrahimbeiati 6d3a0c7
inheritance task
ebrahimbeiati 4ee4f65
Fix age input loop to re-prompt instead of exiting
ebrahimbeiati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #============================================================================== | ||
| # Do not run the following code. | ||
|
|
||
| # This code contains bugs related to types. They are bugs mypy can catch. | ||
|
|
||
| # Read this code to understand what it’s trying to do. Add type annotations to the method parameters and return types of this code. | ||
| # Run the code through mypy, and fix all of the bugs that show up. | ||
| # When you’re confident all of the type annotations are correct, and the bugs are fixed, run the code and check it works. | ||
| #============================================================================== | ||
|
|
||
| def open_account(balances: dict[str, int], name: str, amount: int) -> None: | ||
| balances[name] = amount | ||
|
|
||
| def sum_balances(accounts: dict[str, int]) -> int: | ||
| total = 0 | ||
| for name, pence in accounts.items(): | ||
| print(f"{name} had balance {pence}") | ||
| total += pence | ||
| return total | ||
|
|
||
| def format_pence_as_string(total_pence: int) -> str: | ||
| if total_pence < 100: | ||
| return f"{total_pence}p" | ||
| pounds = int(total_pence / 100) | ||
| pence = total_pence % 100 | ||
| return f"£{pounds}.{pence:02d}" | ||
|
|
||
| balances: dict[str, int] = { | ||
| "Sima": 700, | ||
| "Linn": 545, | ||
| "Georg": 831, | ||
| } | ||
|
|
||
| open_account(balances, "Tobi", 913) | ||
| open_account(balances, "Olya", 713) | ||
|
|
||
| total_pence = sum_balances(balances) | ||
| total_string = format_pence_as_string(total_pence) | ||
|
|
||
| print(f"The bank accounts total {total_string}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #exercise | ||
| # Try changing the type annotation of Person.preferred_operating_system from str to List[str]. | ||
|
|
||
| # Run mypy on the code. | ||
|
|
||
| # It tells us different places that our code is now wrong, because we’re passing values of the wrong type. | ||
|
|
||
| # We probably also want to rename our field - lists are plural. Rename the field to preferred_operating_systems. | ||
|
|
||
| # Run mypy again. | ||
|
|
||
| # Fix all of the places that mypy tells you need changing. Make sure the program works as you’d expect. | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| preferred_operating_systems: List[str] | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Laptop: | ||
| id: int | ||
| manufacturer: str | ||
| model: str | ||
| screen_size_in_inches: float | ||
| operating_system: str | ||
|
|
||
|
|
||
| def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: | ||
| possible_laptops: List[Laptop] = [] | ||
| for laptop in laptops: | ||
| if laptop.operating_system in person.preferred_operating_systems: | ||
| possible_laptops.append(laptop) | ||
| return possible_laptops | ||
|
|
||
|
|
||
| people = [ | ||
| Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu", "Arch Linux"]), | ||
| Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]), | ||
| ] | ||
|
|
||
| laptops = [ | ||
| Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system="Arch Linux"), | ||
| Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="Ubuntu"), | ||
| Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="ubuntu"), | ||
| Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system="macOS"), | ||
| ] | ||
|
|
||
| for person in people: | ||
| possible_laptops = find_possible_laptops(laptops, person) | ||
| print(f"Possible laptops for {person.name}: {possible_laptops}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Exercise 1 — Save the above code to a file, and run it through mypy. | ||
| #Read the error, and make sure you understand what it’s telling you. | ||
|
|
||
| class Person: | ||
| def __init__(self, name: str, age: int, preferred_operating_system: str): | ||
| self.name = name | ||
| self.age = age | ||
| self.preferred_operating_system = preferred_operating_system | ||
|
|
||
| imran = Person("Imran", 22, "Ubuntu") | ||
| print(imran.name) | ||
| print(imran.address) # mypy error: "Person" has no attribute "address" | ||
|
|
||
| eliza = Person("Eliza", 34, "Arch Linux") | ||
| print(eliza.name) | ||
| print(eliza.address) # mypy error: "Person" has no attribute "address" | ||
|
|
||
| # Exercise 2 — Add the is_adult code to the file you saved earlier. | ||
|
|
||
| # Run it through mypy - notice that no errors are reported - | ||
| # mypy understands that Person has a property named age so is happy with the function. | ||
|
|
||
| # Write a new function in the file that accepts a Person as a | ||
| # parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error. | ||
| def is_adult(person: Person) -> bool: | ||
| return person.age >= 18 | ||
|
|
||
| print(is_adult(imran)) # True | ||
|
|
||
| def get_favorite_food(person: Person) -> str: | ||
| return person.favorite_food # mypy error: "Person" has no attribute "favorite_food" | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from dataclasses import dataclass | ||
| from datetime import date | ||
| # exercise | ||
| # Write a Person class using @datatype which uses a datetime.date for date of birth, rather than an int for age. | ||
| # Re-add the is_adult method to it. | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| date_of_birth: date | ||
| preferred_operating_system: str | ||
|
|
||
| def is_adult(self) -> bool: | ||
| today = date.today() | ||
| age = today.year - self.date_of_birth.year | ||
|
|
||
| if(today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day): | ||
| age -= 1 | ||
| return age >= 18 | ||
|
|
||
| imran = Person("Imran", date(2000, 1, 1), "Ubuntu") | ||
| imran2 = Person("Imran", date(2000, 1, 1), "Ubuntu") | ||
| print(imran.is_adult()) | ||
| print(imran == imran2) # True, because dataclasses automatically generate an __eq__ method that compares the fields of the dataclass. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # exercise | ||
| # Write a program which: | ||
|
|
||
| # Already has a list of Laptops that a library has to lend out. | ||
| # Accepts user input to create a new Person - it should use the input function to read a person’s name, | ||
| # age, and preferred operating system. | ||
| # Tells the user how many laptops the library has that have that operating system. | ||
| # If there is an operating system that has more laptops available, tells the user that | ||
| # if they’re willing to accept that operating system they’re more likely to get a laptop. | ||
|
|
||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import List | ||
| import sys | ||
|
|
||
| class OperatingSystem(Enum): | ||
| MACOS = "macOS" | ||
| ARCH = "Arch Linux" | ||
| UBUNTU = "Ubuntu" | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Laptop: | ||
| id: int | ||
| manufacturer: str | ||
| model: str | ||
| screen_size_in_inches: float | ||
| operating_system: OperatingSystem | ||
|
|
||
| laptops: List[Laptop] = [ | ||
| Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), | ||
| Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), | ||
| Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), | ||
| Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), | ||
| ] | ||
|
|
||
| def convert_os(user_input: str) -> OperatingSystem: | ||
| for os_value in OperatingSystem: | ||
| if user_input.lower() == os_value.value.lower(): | ||
| return os_value | ||
| print(f"Error: '{user_input}' is not a valid operating system.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| # Get user input | ||
| name = input("Enter your name: ").strip() | ||
| age_input = input("Enter your age: ").strip() | ||
| if not age_input.isdigit(): | ||
| print(f"Error: '{age_input}' age must be a number.", file=sys.stderr) | ||
| sys.exit(1) | ||
| age = int(age_input) | ||
|
|
||
| os_input = input("Enter your preferred operating system (macOS, Arch Linux, Ubuntu): ").strip() | ||
| preferred_os = convert_os(os_input) | ||
|
|
||
| # Count laptops with the preferred operating system | ||
| matching_laptops = [laptop for laptop in laptops if laptop.operating_system == preferred_os] | ||
| print(f"\nHi {name}, there are {len(matching_laptops)} laptops available with {preferred_os.value}.") | ||
|
|
||
| # Check if there are better laptops with a different operating system | ||
| counts ={os: 0 for os in OperatingSystem} | ||
| for laptop in laptops: | ||
| counts[laptop.operating_system] += 1 | ||
|
|
||
| best_os = max(counts, key=counts.get) | ||
| if best_os != preferred_os: | ||
| print(f"if you’re willing to accept {best_os.value} you’re more likely to get a laptop, as there are {counts[best_os]} available.") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #exercise | ||
| # Fix the above code so that it works. | ||
| # You must not change the print on line 17 - we do want to print the children’s ages. | ||
| # (Feel free to invent the ages of Imran’s children.) | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| children: List["Person"] | ||
|
|
||
| fatma = Person(name="Fatma", age=10, children=[]) | ||
| aisha = Person(name="Aisha", age=5, children=[]) | ||
|
|
||
| imran = Person(name="Imran", age=40, children=[fatma, aisha]) | ||
|
|
||
| def print_family_tree(person: Person) -> None: | ||
| print(person.name) | ||
| for child in person.children: | ||
| print(f"- {child.name} ({child.age})") | ||
|
|
||
| print_family_tree(imran) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| class Parent: | ||
| def __init__(self, first_name: str, last_name: str): | ||
| self.first_name = first_name | ||
| self.last_name = last_name | ||
|
|
||
| def get_name(self) -> str: | ||
| return f"{self.first_name} {self.last_name}" | ||
|
|
||
|
|
||
| class Child(Parent): | ||
| def __init__(self, first_name: str, last_name: str): | ||
| super().__init__(first_name, last_name) | ||
| self.previous_last_names = [] | ||
|
|
||
| def change_last_name(self, last_name) -> None: | ||
| self.previous_last_names.append(self.last_name) | ||
| self.last_name = last_name | ||
|
|
||
| def get_full_name(self) -> str: | ||
| suffix = "" | ||
| if len(self.previous_last_names) > 0: | ||
| suffix = f" (née {self.previous_last_names[0]})" | ||
| return f"{self.first_name} {self.last_name}{suffix}" | ||
|
|
||
| person1 = Child("Elizaveta", "Alekseeva") | ||
| #first name is Elizaveta, last name is Alekseeva, no previous last names | ||
| print(person1.get_name()) | ||
| #Elizaveta Alekseeva | ||
|
|
||
| print(person1.get_full_name()) | ||
| #Elizaveta Alekseeva | ||
|
|
||
| person1.change_last_name("Tyurina") | ||
| #previous last name is now Alekseeva, current last name is Tyurina | ||
|
|
||
| print(person1.get_name()) | ||
| #Elizaveta Tyurina | ||
|
|
||
| print(person1.get_full_name()) | ||
| #Elizaveta Tyurina (née Alekseeva) | ||
|
|
||
| person2 = Parent("Elizaveta", "Alekseeva") | ||
| # first name is Elizaveta, last name is Alekseeva | ||
| print(person2.get_name()) | ||
| #Elizaveta Alekseeva | ||
|
|
||
| print(person2.get_full_name()) | ||
| # AttributeError: 'Parent' object has no attribute 'get_full_name' | ||
| person2.change_last_name("Tyurina") | ||
| # AttributeError: 'Parent' object has no attribute 'change_last_name' | ||
| print(person2.get_name()) | ||
| #Elizaveta Alekseeva, because the last name was not changed | ||
| print(person2.get_full_name()) | ||
| # AttributeError: 'Parent' object has no attribute 'get_full_name' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from datetime import date | ||
| # Exercise 1: | ||
| # Advantages of methods over free functions: | ||
| # 1- Methods keep related data and behavior together | ||
|
|
||
| # 2- Methods make code easier to read (person.is_adult() is clearer than is_adult(person)) | ||
|
|
||
| # 3- Methods reduce the number of parameters (because self already contains the data) | ||
|
|
||
| # 4- Methods help mypy catch more errors (because they belong to a specific class) | ||
|
|
||
| # 5- Methods make the code more object‑oriented and easier to extend | ||
|
|
||
|
|
||
| #Exercise 2 — Change Person to use date of birth | ||
|
|
||
| class Person: | ||
| def __init__(self, name: str, date_of_birth: date, preferred_operating_system: str): | ||
| self.name = name | ||
| self.date_of_birth = date_of_birth | ||
| self.preferred_operating_system = preferred_operating_system | ||
|
|
||
|
|
||
| def is_adult(self): | ||
| today = date.today() | ||
| age = today.year - self.date_of_birth.year | ||
|
|
||
| has_had_birthday_this_year = ( | ||
| (today.month, today.day) >= (self.date_of_birth.month, self.date_of_birth.day) | ||
| ) | ||
| if not has_had_birthday_this_year: | ||
| age -= 1 | ||
| return age >= 18 | ||
|
|
||
| imran = Person("Imran", date(2000, 1, 1), "Ubuntu") | ||
| print(imran.is_adult()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #================================================================================= | ||
| # Exercise 1: Predict what double("22") will do. Then run the code and check. | ||
| # Did it do what you expected? Why did it return the value it did? | ||
| #================================================================================= | ||
| def double(value): | ||
| return value * 2 | ||
|
|
||
| # Prediction: | ||
| # double("22") will return "2222" because when you multiply a string by | ||
| # an integer in Python, it concatenates the string that many times. | ||
| # So "22" * 2 will result in "22" + "22", which is "2222". | ||
|
|
||
| print("Exercise 1: double('22') =", double("22")) | ||
|
|
||
| # Actual output: double('22') = 2222 | ||
|
|
||
| #================================================================================= | ||
| # Identify the bug in double() | ||
| #================================================================================= | ||
| def double_bug(number): | ||
| # Bug: This function is called "double" but multiplies the input by 3 instead of 2. | ||
| # This is a logic error, not a type error | ||
| return number * 3 | ||
|
|
||
| print("Exercise 2:", double_bug(10)) | ||
|
|
||
| #================================================================================= | ||
| # About half(), double(), second() | ||
| #================================================================================= | ||
| def half(value): | ||
| return value / 2 | ||
|
|
||
| def double(value): | ||
| return value * 2 | ||
|
|
||
| def second(value): | ||
| return value[1] | ||
| # Prediction and explanation: | ||
|
|
||
| # half(22) --> 11.0 | ||
| # half("hello") --> TypeError, because you cannot divide a string by a number. | ||
| # half("22") --> TypeError, because you cannot divide a string by a number. | ||
|
|
||
| # double_correct(22) --> 44 | ||
| # double_correct("hello") --> "hellohello", because multiplying a string by an integer concatenates it. | ||
| # double_correct("22") --> "2222" | ||
|
|
||
| # second(22) --> TypeError, because you cannot index an integer. | ||
| # second(0 x 16) --> TypeError, because you cannot index an integer. | ||
| # second("hello") --> "e", because it returns the second character of the string. | ||
| # second("22") --> "2", because it returns the second character of the string. | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to keep asking for input instead of exiting if the input was wrong?