Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
43fd3cc
predict what double("22") will do
eediallo Apr 29, 2025
65c6ee1
add function at limits of type checking section
eediallo Apr 29, 2025
04eb798
change function name to reflect what it does
eediallo Apr 29, 2025
edd621c
add mypy to requirements for type checking
eediallo Apr 29, 2025
58b2ea1
add functions for account management and balance formatting
eediallo Apr 29, 2025
10c2406
add .gitignore to exclude virtual environment directory
eediallo Apr 29, 2025
1c20088
add type annotations to functions and fix account opening parameters
eediallo Apr 29, 2025
d6fc6e4
refactor: update type annotations for account functions and adjust ba…
eediallo Apr 29, 2025
bfb8234
read and understand error
eediallo Apr 29, 2025
d534963
add method to check if a person is an adult
eediallo Apr 29, 2025
e92f78f
Write a function that accepts a Person as a parameter and tries to ac…
eediallo Apr 29, 2025
eab6400
change Person class to take a date of birth and update is_adult method
eediallo Apr 29, 2025
30f8a0f
fix: correct age calculation in is_adult method to account for month …
eediallo Apr 29, 2025
079fa07
update is_adult method to use datetime
eediallo Apr 29, 2025
7ad2748
add ages to Person instances
eediallo Apr 29, 2025
2ea6159
Update Person.preferred_operating_system to use a
eediallo Apr 29, 2025
e787cf2
Add Laptop and Person classes with operating system selection
eediallo Apr 29, 2025
e5cfb61
predict what is going to happen when code is run.
eediallo Apr 30, 2025
07825d2
comments out non existing parent method
eediallo Apr 30, 2025
e9d929f
Refine type annotations for previous_last_names and change_last_name …
eediallo Apr 30, 2025
51bda20
Add node_modules to .gitignore
eediallo Jun 23, 2025
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.venv.
node_modules
22 changes: 22 additions & 0 deletions classes_and_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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(self) -> bool:
return self.age >= 18

imran = Person("Imran", 22, "Ubuntu")
print(imran.name)
# print(imran.address) # address is not an attribute of the Person class
print(imran.is_adult())

eliza = Person("Eliza", 34, "Arch Linux")
print(eliza.name)
# print(eliza.address)# address is not an attribute of the Person class

def get_address(person: Person) -> str:
return person.address

print(get_address(imran))
20 changes: 20 additions & 0 deletions data_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from dataclasses import dataclass
from datetime import date

@dataclass(frozen=True)
class Person:
name: str
date_of_birth: date
preferred_operating_system: str

def is_adult(self) -> bool:
current_date = date.today()
age = current_date.year - self.date_of_birth.year - (
(current_date.month, current_date.day) < (self.date_of_birth.month, self.date_of_birth.day)
)
return age >= 18

# Example usage
imran = Person("Imran", date(2001, 5, 15), "Ubuntu")
print(imran) # Prints Person(name='Imran', date_of_birth=datetime.date(2001, 5, 15), preferred_operating_system='Ubuntu')
print(imran.is_adult()) # Prints True or False depending on the current date
72 changes: 72 additions & 0 deletions enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from dataclasses import dataclass
from enum import Enum

class OperatingSystem(Enum):
MACOS = "macOS"
UBUNTU = "Ubuntu"
ARCH = "Arch Linux"

@dataclass
class Laptop:
id: int
manufacturer: str
model: str
screen_size_in_inches: float
operating_system: OperatingSystem

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),
]

@dataclass
class Person:
name: str
age: int
preferred_operating_system: OperatingSystem


name = input('Please enter your name: ')

age_input = input('Please enter your age: ')
try:
age = int(age_input)
except ValueError:
print(f"Age must be a number")
exit()

preferred_os_input = input('Please enter your preferred operating system: ')
try:
preferred_operating_system = OperatingSystem(preferred_os_input)
except ValueError:
print(f'We are not offering {preferred_os_input}. Please choose from the following list: {[os.value for os in OperatingSystem]}')
exit()



person = Person(name=name, age=age, preferred_operating_system=preferred_operating_system)


number_of_available_laptops = sum(
1 for laptop in laptops if laptop.operating_system == person.preferred_operating_system
)

def offer_laptop_to_user()->None:
offer = input('Would you like a laptop with this OS (yes / no) ? ')
if offer.lower() == "y" or offer.lower() == "yes":
print(f'Great! Please come on monday next week to collect your laptop')
else:
print('No problem. See you later.')

if number_of_available_laptops == 1:
print(f'There is {number_of_available_laptops} laptop available with your preferred operating system.')
offer_laptop_to_user()
elif number_of_available_laptops > 1:
print(f'There are {number_of_available_laptops} laptops available with your preferred operating system.')
offer_laptop_to_user()
else:
print('There are no laptops available with your preferred operating system.')


20 changes: 20 additions & 0 deletions 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=25, children=[])
aisha = Person(name="Aisha",age=30, 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)
37 changes: 37 additions & 0 deletions inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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()) # will thrown an error since get_full_name does not exist in parent class
# person2.change_last_name("Tyurina") # will thrown an error since change_last_name method does not exist in parent class
print(person2.get_name())
# print(person2.get_full_name()) # will thrown an error since get_full_name does not exist in parent class
17 changes: 17 additions & 0 deletions methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from datetime import date

class Person:
def __init__(self, name: str, DoB: date, preferred_operating_system: str):
self.name = name
self.DoB = DoB
self.preferred_operating_system = preferred_operating_system

def is_adult(self)-> bool:
current_date = date.today()
age = current_date.year - self.DoB.year - (
(current_date.month, current_date.day) < (self.DoB.month, self.DoB.day)
)
return age >= 18

imran = Person("Imran", date(1998, 1, 6), "Ubuntu")
print(imran.is_adult())
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mypy
43 changes: 43 additions & 0 deletions type_guided_refac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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:
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", "macOS"]),
Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux", "macOS", "Ubuntu" ]),
]

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

30 changes: 30 additions & 0 deletions type_with_mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def open_account(balances:dict[str, float], name:str, amount:float)-> None:
balances[name] = amount

def sum_balances(accounts:dict[str, float])->float:
total = 0.00
for name, pence in accounts.items():
print(f"{name} had balance {pence}")
total += pence
return total

def format_pence_as_string(total_pence:float) -> 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": 7.00,
"Linn": 5.45,
"Georg": 8.31,
}

open_account(balances, "Tobi", 9.13)
open_account(balances, "Olya", 7.13)

total_pence = sum_balances(balances)
total_string = format_pence_as_string(total_pence)

print(f"The bank accounts total {total_string}")
30 changes: 30 additions & 0 deletions why-we-use-type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def half(value):
return value / 2

def double(value):
return value * 2

def second(value):
return value[1]



# print(half(22))
# print(half("hello"))
# print(half("22"))

# print(double(22))
# print(double("hello"))
# print(double("22"))

# print(second(22))
# print(second(0x16))
# print(second("hello"))
# print(second("22"))

print(double("22")) # will print 2222

def triple(number):
return number * 3

print(triple(10))