Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.venv/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to commit these files, can you remove it / ignore it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Thank you. I tried to remove it by git rm -r --cached pycache

Binary file not shown.
24 changes: 24 additions & 0 deletions prep-exercises/classes_and_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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


def is_adult(person: Person) -> bool:
return person.age >= 18


imran = Person("Imran", 22, "Ubuntu")
print(imran.name)
# print(imran.address)
# Person doesn't have address attribute

eliza = Person("Eliza", 34, "Arch Linux")
print(eliza.name)
# print(eliza.address)
# Person doesn't have address attribute

# def favorite_song(person: Person) -> str:
# return person.song
# error because person doesn't have song attribute
25 changes: 25 additions & 0 deletions prep-exercises/dataclasses_exercise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from dataclasses import dataclass
import datetime as dt

@dataclass(frozen=True)
class Person:
name: str
age: dt.date
preferred_operating_system: str

def is_adult(self) -> bool:
today = dt.datetime.today()

age_in_year = today.year - self.age.year

if (today.month, today.day) < (self.age.month, self.age.day):
age_in_year -= 1
return age_in_year >= 18

imran = Person("Imran", dt.date.fromisoformat("2002-08-05"), "Ubuntu")
print(imran)

imran2 = Person("Imran", dt.date.fromisoformat("2002-08-05"), "Ubuntu")
print(imran == imran2)

print(f'{imran.name} is adult: {imran.is_adult()}')
80 changes: 80 additions & 0 deletions prep-exercises/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from dataclasses import dataclass
from enum import Enum
from typing import List, Dict
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


def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
possible_laptops = []
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),
]

def user_register() -> Person:

user_name: str = input("Please enter your name: ")
user_age_str: str = input("Please enter your age: ")

try:
user_age: int = int(user_age_str)
if user_age < 0:
raise ValueError
except ValueError:
sys.stderr.write(f'Error: "{user_age_str}" is not a valid age. \n')
sys.exit(1)

user_system_str = input("Please enter your preferred operating system - Arch Linux, Ubuntu, or macOS: ")

try:
user_system = OperatingSystem(user_system_str)
except ValueError:
sys.stderr.write(f'Error: "{user_system_str}" is not a valid system. \n')
sys.exit(1)

return Person(name=user_name, age=user_age, preferred_operating_system=user_system)


def matching_laptop(new_user: Person = user_register()) -> None:
possible_laptops = find_possible_laptops(laptops=laptops, person=new_user)

print(f'Library has {len(possible_laptops)} laptops have {new_user.preferred_operating_system.value}')

system_counts: Dict[OperatingSystem, int] = {}
for laptop in laptops:
system_counts[laptop.operating_system] = (system_counts.get(laptop.operating_system) or 0 ) + 1

most_available = max(system_counts, key=lambda name: system_counts[name])

if system_counts[most_available] > len(possible_laptops):
print(f'if you are willing to accept {most_available.value} you are more likely to get a laptop.')


matching_laptop()
20 changes: 20 additions & 0 deletions prep-exercises/generics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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=12, children=[])

imran = Person(name="Imran", age=50, 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)
50 changes: 50 additions & 0 deletions prep-exercises/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import List

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")
print(person1.get_name())
print(person1.get_full_name())
person1.change_last_name("Tyurina")
print(person1.get_name())
print(person1.get_full_name())

person2 = Parent("Elizaveta", "Alekseeva")
print(person2.get_name())
# print(person2.get_full_name())
# person2.change_last_name("Tyurina")
print(person2.get_name())
# print(person2.get_full_name())


# Predictions:
# person 1 should be able to print both name and full name as Elizaveta Alekseeva
# and should be able to print both name Elizaveta Tyurina and full name Elizaveta Tyurina (née Alekseeva) after changing last name to Tyurina
# because it was created in class Child which inherits get_name from class Parent
#
# In contracts, person 2 is able to print name as Elizaveta Alekseeva
# but shouldn't be able to print full name as it was create in class Parent, which does not have method get_full_name
# and couldn't be able to change last name to Tyurina, so the name stays the same Elizaveta Alekseeva
# and it still won't be able to print full name even after the attempt of change last name
23 changes: 23 additions & 0 deletions prep-exercises/methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Think of the advantages of using methods instead of free functions. Write them down in your notebook.
# It is object oriented. The same method can be shared between the same class.
# It is more obvious to understand the purpose of the methods than a free function

import datetime as dt

class Person:
def __init__(self, name: str, date_of_birth: str, preferred_operating_system: str):
self.name = name
self.date_of_birth = dt.date.fromisoformat(date_of_birth)
self.preferred_operating_system = preferred_operating_system

def is_adult(self) -> bool:
today = dt.datetime.today()

age_in_year = today.year - self.date_of_birth.year

if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
age_in_year -= 1
return age_in_year >= 18

imran = Person("Imran", "2000-10-20", "Ubuntu")
print(imran.is_adult())
32 changes: 32 additions & 0 deletions prep-exercises/type_checking_with_mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Dict

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, "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}")
57 changes: 57 additions & 0 deletions prep-exercises/type_guided_refactorings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 = []
for laptop in laptops:
for system in person.preferred_operating_systems:
if laptop.operating_system == system:
possible_laptops.append(laptop)
return possible_laptops


people = [
Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu", "Arch Linux", "macOS", "Windows"]),
Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux", "Ubuntu", "macOS", "Windows"]),
]

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}")

system_list = person.preferred_operating_systems
preference_sentences: List[str] = ["prefers {} most of all", ", and then {}", ", but will not use {}"]
statement: str = f'{person.name} '

if len(system_list) != 0:
for index, system in enumerate(system_list):
if index == 0:
statement += preference_sentences[index].format(system)
elif index == len(system_list) - 1:
statement += preference_sentences[-1].format(system)
else:
statement += preference_sentences[1].format(system)
print(statement)
9 changes: 9 additions & 0 deletions prep-exercises/why_we_use_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def double(number: float) -> float:
return number * 2

print(double(10))

# The code above was not defined with types for the function and variable
# I should add the types to fix the code
# Besides the function name did not reflect its return
# I make the return as number * 2 instead
Loading