-
-
Notifications
You must be signed in to change notification settings - Fork 94
London | 26-SDC-March | Zobeir Rigi | Sprint 5 | prep-exercises #536
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
Zobeir-Rigi
wants to merge
11
commits into
CodeYourFuture:main
Choose a base branch
from
Zobeir-Rigi:prep-exercises
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
77f43b1
double the string
Zobeir-Rigi 16e732d
triple function instead of double
Zobeir-Rigi 97fd02e
Add type annotations and fix mypy errors
Zobeir-Rigi 8a39ff1
attribute err, person dont have att address
Zobeir-Rigi 31c4467
type checking with mypy
Zobeir-Rigi fe6461f
update person to use date of birth instead of age
Zobeir-Rigi d4db11c
implement Person with dataclass
Zobeir-Rigi 57a5263
fixed bug by adding age att
Zobeir-Rigi accbd37
finding problems with mypy, and refactoring
Zobeir-Rigi e983614
enum
Zobeir-Rigi 21aec73
learned about inheritance
Zobeir-Rigi 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,34 @@ | ||
| 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) | ||
|
|
||
| eliza = Person("Eliza", 34, "Arch Linux") | ||
| print(eliza.name) | ||
| print(eliza.address) | ||
|
|
||
| def is_adult(person: Person) -> bool: | ||
| return person.age >= 18 | ||
|
|
||
| print(is_adult(imran)) | ||
|
|
||
| def get_id(person: Person) -> int: | ||
| return person.id | ||
| # Exercise | ||
| # 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. | ||
| # Answer : "Person" has no attribute "address". both instances of "Person", and they dont have address att. | ||
|
|
||
| # Exercise | ||
| # 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. | ||
|
|
||
| # Answer : "Person" has no attribute "id". |
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,33 @@ | ||
| # ✍️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. | ||
|
|
||
| from dataclasses import dataclass | ||
| from datetime import date | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name:str | ||
| date_of_birth: date | ||
| prefered_o_s: 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 | ||
|
|
||
|
|
||
| imran1 = Person("Imran", date(2002, 5, 10), "Ubuntu") | ||
| imran2 = Person("Imran", date(2002, 5, 10), "Ubuntu") | ||
|
|
||
| print(imran1 == imran2) | ||
|
|
||
| print(imran1.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,94 @@ | ||
| 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 Person: | ||
| name: str | ||
| age: int | ||
| preferred_operating_system: OperatingSystem | ||
|
|
||
|
|
||
| @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 parse_os(user_input: str) -> OperatingSystem: | ||
| for os in OperatingSystem: | ||
| if os.value.lower() == user_input.lower(): | ||
| return os | ||
| raise ValueError("Invalid operating system") | ||
|
|
||
|
|
||
| def count_laptops(os: OperatingSystem) -> int: | ||
| return sum(1 for laptop in laptops if laptop.operating_system == os) | ||
|
|
||
|
|
||
|
|
||
| def most_available_os() -> OperatingSystem: | ||
| return max(OperatingSystem, key=lambda os: count_laptops(os)) | ||
|
|
||
|
|
||
|
|
||
| # Main program | ||
| try: | ||
| name = input("Enter your name: ").strip() | ||
|
|
||
| age_input = input("Enter your age: ").strip() | ||
| age = int(age_input) | ||
| if age < 0: | ||
| raise ValueError("Age must be a positive number") | ||
|
|
||
| os_input = input("Enter OS (macOS, Arch Linux, Ubuntu): ").strip() | ||
| preferred_os = parse_os(os_input) | ||
|
|
||
| except ValueError as e: | ||
| print(f"Error: {e}", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| person = Person(name, age, preferred_os) | ||
|
|
||
| available = count_laptops(person.preferred_operating_system) | ||
| print(f"\nWe have {available} laptops with {person.preferred_operating_system.value}.") | ||
|
|
||
| best = most_available_os() | ||
| if best != person.preferred_operating_system: | ||
| more = count_laptops(best) | ||
| print(f"If you're flexible, {best.value} has more laptops available ({more}).") | ||
|
|
||
|
|
||
|
|
||
| # ✍️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. | ||
| # You should convert the age and preferred operating system input from the user into more | ||
| # constrained types as quickly as possible, and should output errors to stderr and terminate | ||
| # the program with a non-zero exit code if the user input bad values. | ||
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,21 @@ | ||
| # ✍️exercise | ||
| # Predict what double("22") will do. Then run the code and check. | ||
| # Did it do what you expected? no | ||
| # Why did it return the value it did? it just concatenated (repeated) the string. I expected it returns 44, but there is type issue. | ||
|
|
||
|
|
||
| def double(value): | ||
| return value * 2 | ||
|
|
||
| print(double("22")) | ||
|
|
||
| # --------------- | ||
| def double(number): | ||
| return number * 3 | ||
|
|
||
| print(double(10)) | ||
|
|
||
| # ✍️exercise | ||
| # Read the above code and write down what the bug is. How would you fix it? | ||
| # it will return 30, but the function name is double, so we have to change the function name to triple or change the return statement to number * 2. | ||
|
|
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,23 @@ | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| children: List["Person"] | ||
| age: int | ||
|
|
||
| fatma = Person(name="Fatma", children=[], age=10) | ||
| aisha = Person(name="Aisha", children=[], age=8) | ||
|
|
||
| imran = Person(name="Imran", children=[fatma, aisha], age=35) | ||
|
|
||
| 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) | ||
|
|
||
| # 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.) |
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,78 @@ | ||
| # ✍️exercise | ||
| # Play computer with this code. Predict what you expect each line will do. Then run the code and check your predictions. | ||
| # (If any lines cause errors, you may need to comment them out to check later lines). | ||
|
|
||
| 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}" | ||
|
|
||
| # Child class (inherits from Parent) | ||
| class Child(Parent): | ||
| def __init__(self, first_name: str, last_name: str): | ||
| super().__init__(first_name, last_name) # Call Parent constructor | ||
| self.previous_last_names: list[str] = [] # New property (only Child has this) | ||
|
|
||
|
|
||
| def change_last_name(self, last_name) -> None: | ||
| self.previous_last_names.append(self.last_name) # Save old last name | ||
| self.last_name = last_name # Update to new last name | ||
|
|
||
| def get_full_name(self) -> str: # Extra method only in Child | ||
| 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") | ||
| print(person1.get_name()) # Inherited method from Parent | ||
| # Output: Elizaveta Alekseeva | ||
|
|
||
| print(person1.get_full_name()) | ||
| # Method from Child , | ||
| # Output: Elizaveta Alekseeva | ||
|
|
||
|
|
||
| person1.change_last_name("Tyurina") | ||
| # Change last name | ||
|
|
||
| print(person1.get_name()) | ||
| # Parent method still works , | ||
| # Output: Elizaveta Tyurina (née Alekseeva) | ||
|
|
||
| print(person1.get_full_name()) | ||
|
|
||
|
|
||
| # Using Parent | ||
| person2 = Parent("Elizaveta", "Alekseeva") | ||
|
|
||
| # Works (Parent method), | ||
| # Output: Elizaveta Alekseeva | ||
| print(person2.get_name()) | ||
|
|
||
|
|
||
| # print(person2.get_full_name()) | ||
| # ERROR: Parent has no method "get_full_name" | ||
|
|
||
| # person2.change_last_name("Tyurina") | ||
| # ERROR: Parent has no method "change_last_name" | ||
|
|
||
| print(person2.get_name()) | ||
| # print(person2.get_full_name()) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| # Inheritance allows a class (Child) to reuse and extend another class (Parent). | ||
| # When calling a method, Python first checks the subclass, then the superclass. | ||
| # The Child can add or override methods, but the Parent cannot access Child-specific methods. | ||
|
|
||
| # Difference between inheritance and composition: | ||
| # - Inheritance: "is-a" (Child IS a Parent) | ||
| # - Composition: "has-a" (Object HAS another object) |
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,44 @@ | ||
| # ✍️exercise | ||
| # Change the Person class to take a date of birth (using the standard library’s datetime.date class) and store it in a field instead of age. | ||
| # Update the is_adult method to act the same as before. | ||
| from datetime import date | ||
| 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) -> 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(2002, 5, 10), "Ubuntu") | ||
| eliza = Person("Eliza", date(1990, 8, 3), "Arch Linux") | ||
|
|
||
| print(imran.is_adult()) | ||
| print(eliza.is_adult()) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| # ✍️exercise | ||
| # Think of the advantages of using methods instead of free functions. | ||
| # Write them down in your notebook. | ||
| # Encapsulation | ||
| # The class controls its own logic | ||
| # Example: You change from age → date_of_birth | ||
| # Only the class needs to change, External code still works:person.is_adult() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you expand on this point, what do you mean by "external code still works"? |
||
| # Methods = behavior belongs to the object | ||
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,38 @@ | ||
| # ✍️exercise | ||
| # 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 = { | ||
| "Sima": 700, | ||
| "Linn": 545, | ||
| "Georg": 831, | ||
| } | ||
|
|
||
| open_account(balances, name="Tobi", amount=913) | ||
| open_account(balances, name="Olya", amount=713) | ||
|
|
||
| total_pence = sum_balances(balances) | ||
| total_string = format_pence_as_string(total_pence) | ||
|
|
||
| print(f"The bank accounts total {total_string}") |
Oops, something went wrong.
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.
If you get an input error, is there a user friendly way to handle this without exiting the program?