diff --git a/.gitignore b/.gitignore index 3c3629e64..274d04915 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.venv \ No newline at end of file diff --git a/prep-exercises/classes_and_objects.py b/prep-exercises/classes_and_objects.py new file mode 100644 index 000000000..0b7738b92 --- /dev/null +++ b/prep-exercises/classes_and_objects.py @@ -0,0 +1,37 @@ +# ------------------------ +# Q. 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) + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) +# print(eliza.address) + +# A. The error is telling me that the 'address' attribute cannot be accessed because it does not exist on the 'Person' class + + +def is_adult(person: Person) -> bool: + return person.age >= 18 + + +print(is_adult(imran)) + + +# ------------------------ +# Q. 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 get_address(person: Person) -> str: +# return person.address + +# A. As in the previous example, the attribute cannot be accessed because it does not exist on the 'Person' class diff --git a/prep-exercises/dataclasses_ex.py b/prep-exercises/dataclasses_ex.py new file mode 100644 index 000000000..9b18d4633 --- /dev/null +++ b/prep-exercises/dataclasses_ex.py @@ -0,0 +1,30 @@ +# ------------------------ +# Q. 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. +# ------------------------ + +# A. + +import datetime as dt +from dataclasses import dataclass + + +@dataclass +class Person: + name: str + birthdate: dt.date + preferred_operating_system: str + + def is_adult(self) -> bool: + today = dt.date.today() + age = today.year - self.birthdate.year + birthday_this_year = dt.date( + today.year, self.birthdate.month, self.birthdate.day) + if today < birthday_this_year: + age -= 1 + return age >= 18 + + +imran = Person("Imran", dt.date(2008, 3, 21), "Ubuntu") +print(imran.is_adult()) diff --git a/prep-exercises/enums.py b/prep-exercises/enums.py new file mode 100644 index 000000000..58cf3cbfc --- /dev/null +++ b/prep-exercises/enums.py @@ -0,0 +1,100 @@ +# ------------------------ +# 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. +# ------------------------ + +# A. + +import sys +from dataclasses import dataclass +from enum import Enum +from typing import List + + +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 + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + possible_laptops: List[Laptop] = [] + for laptop in laptops: + if laptop.operating_system == person.preferred_operating_system: + possible_laptops.append(laptop) + return possible_laptops + + +laptops = [ + 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), +] + +try: + user_name = input('Enter your name: ') + if not user_name.strip(): + raise ValueError("Name cannot be empty") +except ValueError: + sys.stderr.write("Error: Please provide a valid name\n") + sys.exit(1) + +try: + user_age_input = input('Enter your age: ') + user_age = int(user_age_input) + if user_age < 0: + raise ValueError("Age cannot be negative") +except ValueError: + sys.stderr.write("Error: Please provide a valid age\n") + sys.exit(1) + +try: + user_os_input = input( + 'Enter your preferred operating system (macOS, Arch Linux, Ubuntu): ') + user_os = OperatingSystem(user_os_input) +except ValueError: + sys.stderr.write("Error: Please enter a valid operating system\n") + sys.exit(1) + +person = Person(name=user_name, age=user_age, + preferred_operating_system=user_os) + +possible_laptops = find_possible_laptops(laptops, person) +print(f"We have {len(possible_laptops)} laptop(s) with {user_os.value}") + +# Find OS with most laptops +os_counts: dict[OperatingSystem, int] = {} +for laptop in laptops: + os_counts[laptop.operating_system] = os_counts.get( + laptop.operating_system, 0) + 1 + +best_os = max(os_counts, key=lambda os: os_counts[os]) +if best_os != user_os and os_counts[best_os] > len(possible_laptops): + print( + f"If you're willing to accept {best_os.value}, you're more likely to get a laptop ({os_counts[best_os]} available)") diff --git a/prep-exercises/generics.py b/prep-exercises/generics.py new file mode 100644 index 000000000..a5df7edc9 --- /dev/null +++ b/prep-exercises/generics.py @@ -0,0 +1,30 @@ +# ------------------------ +# 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.) +# ------------------------ + +# A. + +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=11, children=[]) +aisha = Person(name="Aisha", age=6, children=[]) + +imran = Person(name="Imran", age=43, 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) diff --git a/prep-exercises/inheritance.py b/prep-exercises/inheritance.py new file mode 100644 index 000000000..4268d4d5a --- /dev/null +++ b/prep-exercises/inheritance.py @@ -0,0 +1,66 @@ +# ------------------------ +# 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). +# ------------------------ + +# A. + +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: list[str] = [] + + def change_last_name(self, last_name: str) -> 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") +# Predict: person1 has first_name="Elizaveta", last_name="Alekseeva", previous_last_names=[] + +print(person1.get_name()) +# Checked: prints "Elizaveta Alekseeva" + +print(person1.get_full_name()) +# Predict + checked: prints "Elizaveta Alekseeva" + +person1.change_last_name("Tyurina") +# Predict: previous_last_names becomes ["Alekseeva"], last_name becomes "Tyurina" + +print(person1.get_name()) +# Predict + checked: prints "Elizaveta Tyurina" + +print(person1.get_full_name()) +# Predict + checked: prints "Elizaveta Tyurina (née Alekseeva)" + +person2 = Parent("Elizaveta", "Alekseeva") +# Predict: person2 has first_name and last_name only + +print(person2.get_name()) +# Predict + checked: prints "Elizaveta Alekseeva" + +# print(person2.get_full_name()) +# Predict + checked: AttributeError, because Parent has no get_full_name method + +# person2.change_last_name("Tyurina") +# Predict + checked: AttributeError, because Parent has no change_last_name method + +print(person2.get_name()) +# Predict + checked: still prints "Elizaveta Alekseeva" + +# print(person2.get_full_name()) +# Predict + checked: AttributeError again for missing Parent.get_full_name diff --git a/prep-exercises/methods.py b/prep-exercises/methods.py new file mode 100644 index 000000000..b89a3cbe5 --- /dev/null +++ b/prep-exercises/methods.py @@ -0,0 +1,39 @@ +# ------------------------ +# Q. Think of the advantages of using methods instead of free functions. +# ------------------------ + +# A. +# - Keeps related data together +# - Dot notation makes it clear what the operation is acting on +# - Doesn't pollute global namespace +# - Easier to maintain since all methods are inside the Class, whereas free functions can be scattered across different modules + +# ------------------------ +# Q. 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. +# ------------------------ + +# A. + +import datetime as dt + + +class Person: + def __init__(self, name: str, birthdate: dt.date, preferred_operating_system: str): + self.name = name + self.birthdate = birthdate + self.preferred_operating_system = preferred_operating_system + + def is_adult(self) -> bool: + today = dt.date.today() + age = today.year - self.birthdate.year + birthday_this_year = dt.date( + today.year, self.birthdate.month, self.birthdate.day) + if today < birthday_this_year: + age -= 1 + return age >= 18 + + +imran = Person("Imran", dt.date(2008, 3, 22), "Ubuntu") +print(imran.is_adult()) diff --git a/prep-exercises/type_checking.py b/prep-exercises/type_checking.py new file mode 100644 index 000000000..9529d8bbb --- /dev/null +++ b/prep-exercises/type_checking.py @@ -0,0 +1,43 @@ +# ------------------------ +# Q. 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): + balances[name] = amount + + +def sum_balances(accounts: dict[str, 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): + 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, +} + +# Added missing argument for balances +# Converted float and string arguments to integers +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}") diff --git a/prep-exercises/type_guided_refactorings.py b/prep-exercises/type_guided_refactorings.py new file mode 100644 index 000000000..5debbd304 --- /dev/null +++ b/prep-exercises/type_guided_refactorings.py @@ -0,0 +1,68 @@ +# ------------------------ +# Q. 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. + +# ------------------------ + +# A. + +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: + for operating_system in person.preferred_operating_systems: + + if laptop.operating_system == operating_system: + 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", "macOS"]), +] + +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}") diff --git a/prep-exercises/why_we_use_types.py b/prep-exercises/why_we_use_types.py new file mode 100644 index 000000000..966bdab11 --- /dev/null +++ b/prep-exercises/why_we_use_types.py @@ -0,0 +1,31 @@ +# ------------------------ +# Q. 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? +# ------------------------ + +# A. I predicted that the return value of double("22") would be 44, because I know that in JavaScript '*' is a numeric operator and would coerce "22" into the number 22. +# It did not return what I expected and instead repeated the string twice, this is because in Python strings can be multiplied by integers to repeat them + +# def double(value): +# return value * 2 + + +# print(double("22")) # Returns "2222" + + +# ------------------------ +# Q. Read the above code and write down what the bug is. How would you fix it? +# ------------------------ + +# def double(number): +# return number * 3 + +# print(double(10)) # Returns 30 + +# A. The bug is that the return value is triple the input value +# assuming the function is named correctly, it be can be fixed with this implemntation; + +# def double(number): +# return number * 2 + + +# print(double(10)) # Returns 20