Skip to content

Commit e6858b5

Browse files
author
Xinxi
committed
inheritance.py
1 parent e217305 commit e6858b5

4 files changed

Lines changed: 170 additions & 0 deletions

File tree

Sprint5/enums.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import sys
2+
from dataclasses import dataclass
3+
from enum import Enum
4+
from typing import List
5+
6+
class OperatingSystem(Enum):
7+
MACOS = "macOS"
8+
ARCH = "Arch Linux"
9+
UBUNTU = "Ubuntu"
10+
11+
@dataclass(frozen=True)
12+
class Laptop:
13+
id: int
14+
operating_system: OperatingSystem
15+
16+
@dataclass(frozen=True)
17+
class Person:
18+
name: str
19+
age: int
20+
preferred_operating_system: OperatingSystem
21+
22+
laptops = [
23+
Laptop(id=1, operating_system=OperatingSystem.ARCH),
24+
Laptop(id=2, operating_system=OperatingSystem.UBUNTU),
25+
Laptop(id=3, operating_system=OperatingSystem.UBUNTU),
26+
Laptop(id=4, operating_system=OperatingSystem.MACOS),
27+
Laptop(id=5, operating_system=OperatingSystem.UBUNTU),
28+
]
29+
30+
def get_user_person() -> Person:
31+
name = input("Enter your name: ")
32+
33+
try:
34+
age = int(input("Enter your age: "))
35+
except ValueError:
36+
print("Error: Age must be a number.", file=sys.stderr)
37+
sys.exit(1)
38+
39+
os_input = input("Enter preferred OS (macOS, Arch Linux, Ubuntu): ")
40+
try:
41+
pref_os = OperatingSystem(os_input)
42+
except ValueError:
43+
print(f"Error: '{os_input}' is not a valid operating system.", file=sys.stderr)
44+
sys.exit(1)
45+
return Person(name=name, age=age, preferred_operating_system=pref_os)
46+
47+
def main():
48+
user = get_user_person()
49+
counts = {os: 0 for os in OperatingSystem}
50+
for laptop in laptops:
51+
counts[laptop.operating_system] += 1
52+
53+
user_pref = user.preferred_operating_system
54+
pref_count = counts[user_pref]
55+
print(f"There are {pref_count} laptops available with {user_pref.value}.")
56+
57+
best_os = max(counts, key=counts.get)
58+
max_count = counts[best_os]
59+
60+
if max_count > pref_count:
61+
print(f"Note: If you are willing to use {best_os.value}, we have {max_count} available. "
62+
"You're more likely to get one!")
63+
64+
if __name__ == "__main__":
65+
main()

Sprint5/generics.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from dataclasses import dataclass
2+
from typing import List
3+
4+
@dataclass(frozen=True)
5+
class Person:
6+
name: str
7+
age: int # Added the missing field
8+
children: List["Person"]
9+
10+
# Inventing ages for the family members
11+
fatma = Person(name="Fatma", age=8, children=[])
12+
aisha = Person(name="Aisha", age=10, children=[])
13+
14+
# Imran needs an age as well, and now includes his children
15+
imran = Person(name="Imran", age=40, children=[fatma, aisha])
16+
17+
def print_family_tree(person: Person) -> None:
18+
print(person.name)
19+
for child in person.children:
20+
# This line now works because 'age' exists in the Person class
21+
print(f"- {child.name} ({child.age})")
22+
23+
print_family_tree(imran)

Sprint5/inheritance.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Parent:
2+
def __init__(self, first_name: str, last_name: str):
3+
self.first_name = first_name
4+
self.last_name = last_name
5+
6+
def get_name(self) -> str:
7+
return f"{self.first_name} {self.last_name}"
8+
9+
10+
class Child(Parent):
11+
def __init__(self, first_name: str, last_name: str):
12+
super().__init__(first_name, last_name)
13+
self.previous_last_names = []
14+
15+
def change_last_name(self, last_name) -> None:
16+
self.previous_last_names.append(self.last_name)
17+
self.last_name = last_name
18+
19+
def get_full_name(self) -> str:
20+
suffix = ""
21+
if len(self.previous_last_names) > 0:
22+
suffix = f" (née {self.previous_last_names[0]})"
23+
return f"{self.first_name} {self.last_name}{suffix}"
24+
25+
person1 = Child("Elizaveta", "Alekseeva")
26+
print(person1.get_name())
27+
print(person1.get_full_name())
28+
person1.change_last_name("Tyurina")
29+
print(person1.get_name())
30+
print(person1.get_full_name())
31+
32+
person2 = Parent("Elizaveta", "Alekseeva")
33+
print(person2.get_name())
34+
print(person2.get_full_name())
35+
person2.change_last_name("Tyurina")
36+
print(person2.get_name())
37+
print(person2.get_full_name())
38+
39+
# The Child can access everything the Parent has because it inherits from it.
40+
# The parent has no attribute of the Child.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from dataclasses import dataclass
2+
from typing import List
3+
4+
@dataclass(frozen=True)
5+
class Person:
6+
name: str
7+
age: int
8+
preferred_operating_systems: List[str]
9+
10+
11+
@dataclass(frozen=True)
12+
class Laptop:
13+
id: int
14+
manufacturer: str
15+
model: str
16+
screen_size_in_inches: float
17+
operating_system: str
18+
19+
20+
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
21+
possible_laptops = []
22+
for laptop in laptops:
23+
if laptop.operating_system in person.preferred_operating_systems:
24+
possible_laptops.append(laptop)
25+
return possible_laptops
26+
27+
28+
people = [
29+
Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu", "Arch Linux"]),
30+
Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]),
31+
]
32+
33+
laptops = [
34+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system="Arch Linux"),
35+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="Ubuntu"),
36+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="ubuntu"),
37+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system="macOS"),
38+
]
39+
40+
for person in people:
41+
possible_laptops = find_possible_laptops(laptops, person)
42+
print(f"Possible laptops for {person.name}: {possible_laptops}")

0 commit comments

Comments
 (0)