Skip to content

Commit 1c3d31b

Browse files
committed
made reviewed changes
1 parent 628383c commit 1c3d31b

4 files changed

Lines changed: 77 additions & 32 deletions

File tree

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# Think of the advantages of using methods instead of free functions. Write them down in your notebook.
22

33
# Better readability of code
4-
# Encapsulation of that function only to class - cleaner and better security.
4+
# - Method names can be simplified as they only apply to that class, they are contained within the class which is more
5+
# organised and method names can be reused in different classes.
6+
7+
# Encapsulation of that function only to class - cleaner and better security.
8+
# - This can allow for discoverability of methods when writing code using . in an IDE.
9+
# - Data can be protected from outside functions changing it, this allows for control over what can be changed using
10+
# only the inbuilt methods of a class
11+
12+
# Potential drawback: For a function that may want to be used on class objects from multiple different classes it may
13+
# be better to use a function instead of repeated a method for each class.
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import date
22

3+
34
class Person:
45
def __init__(self, name: str, dob: date, preferred_operating_system: str):
56
self.name = name
@@ -9,12 +10,10 @@ def __init__(self, name: str, dob: date, preferred_operating_system: str):
910
def is_adult(self):
1011
dob = self.dob
1112
today = date.today()
12-
if dob.year > today.year - 18: return False
13-
if dob.year == today.year - 18:
14-
if dob.month > today.month: return False
15-
if dob.month == today.month:
16-
if dob.day > today.day: return False
17-
return True
13+
today_tuple = (today.year, today.month, today.day)
14+
eighteenth_bday_tuple = (dob.year + 18, dob.month, dob.day)
15+
16+
return today_tuple >= eighteenth_bday_tuple
1817

1918

2019
imran = Person("Imran", date(2004, 8, 31), "Ubuntu")
@@ -23,4 +22,4 @@ def is_adult(self):
2322
# Change the Person class to take a date of birth
2423
# (using the standard library’s datetime.date class) and store it in a field instead of age.
2524

26-
# Update the is_adult method to act the same as before.
25+
# Update the is_adult method to act the same as before.

‎sprint-5-prep-exercises/exercise_10.py‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from dataclasses import dataclass
2-
from typing import List
2+
33

44
@dataclass(frozen=True)
55
class Person:
66
name: str
77
age: int
8-
preferred_operating_systems: List[str]
8+
preferred_operating_systems: list[str]
99

1010

1111
@dataclass(frozen=True)
@@ -17,10 +17,10 @@ class Laptop:
1717
operating_system: str
1818

1919

20-
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
20+
def find_possible_laptops(laptops: list[Laptop], person: Person) -> list[Laptop]:
2121
possible_laptops = []
2222
for laptop in laptops:
23-
if laptop.operating_system == person.preferred_operating_systems:
23+
if laptop.operating_system in person.preferred_operating_systems:
2424
possible_laptops.append(laptop)
2525
return possible_laptops
2626

@@ -31,10 +31,34 @@ def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]
3131
]
3232

3333
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"),
34+
Laptop(
35+
id=1,
36+
manufacturer="Dell",
37+
model="XPS",
38+
screen_size_in_inches=13,
39+
operating_system="Arch Linux",
40+
),
41+
Laptop(
42+
id=2,
43+
manufacturer="Dell",
44+
model="XPS",
45+
screen_size_in_inches=15,
46+
operating_system="Ubuntu",
47+
),
48+
Laptop(
49+
id=3,
50+
manufacturer="Dell",
51+
model="XPS",
52+
screen_size_in_inches=15,
53+
operating_system="Ubuntu",
54+
),
55+
Laptop(
56+
id=4,
57+
manufacturer="Apple",
58+
model="macBook",
59+
screen_size_in_inches=13,
60+
operating_system="macOS",
61+
),
3862
]
3963

4064
for person in people:
@@ -56,4 +80,4 @@ def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]
5680
# exercise_10.py:29: error: Unexpected keyword argument "preferred_operating_system" for "Person"; did you mean "preferred_operating_systems"? [call-arg]
5781
# exercise_10.py:30: error: Unexpected keyword argument "preferred_operating_system" for "Person"; did you mean "preferred_operating_systems"? [call-arg]
5882

59-
# Fix all of the places that mypy tells you need changing. Make sure the program works as you’d expect.
83+
# Fix all of the places that mypy tells you need changing. Make sure the program works as you’d expect.

‎sprint-5-prep-exercises/exercise_11.py‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,57 @@
55
# Tells the user how many laptops the library has that have that operating system.
66
# 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.
77

8-
# You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible,
8+
# You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible,
99
# and should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values.
1010

1111
# ----- Imports
12+
import sys
1213
from dataclasses import dataclass
1314
from enum import Enum
14-
import sys
1515

16-
# ----- Classes and Enums
16+
17+
# ----- Classes and Enums
1718
class OperatingSystem(Enum):
1819
MACOS = "macOS"
1920
ARCH = "Arch Linux"
2021
UBUNTU = "Ubuntu"
2122
WINDOWS = "Windows"
2223

24+
2325
@dataclass(frozen=True)
2426
class Person:
2527
name: str
2628
age: int
2729
preferred_operating_system: OperatingSystem
2830

31+
2932
@dataclass(frozen=True)
3033
class Laptop:
3134
id: int
3235
operating_system: OperatingSystem
3336

34-
# ----- Functions
37+
38+
# ----- Functions
3539
def count_laptops(laptops: list[Laptop], laptop_counts: dict[str, int]):
3640
for laptop in laptops:
37-
laptop_counts[laptop.operating_system] += 1
41+
laptop_counts[laptop.operating_system] += 1
42+
3843

3944
def check_laptop_abundance(user: Person, laptop_counts: dict[OperatingSystem, int]):
4045
abundant_laptops = []
4146
for laptop in laptop_counts:
4247
if laptop_counts[laptop] > laptop_counts[user.preferred_operating_system]:
4348
abundant_laptops.append(laptop.value)
4449
if len(abundant_laptops) > 0:
45-
print("\nIf you are willing to accept another operating system you may get a laptop sooner.")
50+
print(
51+
"\nIf you are willing to accept another operating system you may get a laptop sooner."
52+
)
4653
print("We have more laptops available with the following OS:")
4754
for laptop in abundant_laptops:
4855
print(laptop)
49-
50-
# ----- Data and Constants
56+
57+
58+
# ----- Data and Constants
5159
laptops = [
5260
Laptop(id=1, operating_system=OperatingSystem.ARCH),
5361
Laptop(id=2, operating_system=OperatingSystem.ARCH),
@@ -62,10 +70,10 @@ def check_laptop_abundance(user: Person, laptop_counts: dict[OperatingSystem, in
6270
OperatingSystem.MACOS: 0,
6371
OperatingSystem.ARCH: 0,
6472
OperatingSystem.UBUNTU: 0,
65-
OperatingSystem.WINDOWS: 0
73+
OperatingSystem.WINDOWS: 0,
6674
}
6775

68-
# ----- Script
76+
# ----- Script
6977
user_name = input("Please enter your full name:\n")
7078
user_age_str = input("Please enter your age:\n")
7179

@@ -74,15 +82,20 @@ def check_laptop_abundance(user: Person, laptop_counts: dict[OperatingSystem, in
7482
except ValueError:
7583
sys.exit("Error: Age should be a number.")
7684

77-
user_operating_system_str = input("Please enter your preferred operating system (options: ARCH, UBUNTU, MACOS, WINDOWS):\n")
85+
user_operating_system_str = input(
86+
"Please enter your preferred operating system (options: Arch, Ubuntu, MacOS, Windows):\n"
87+
).upper()
7888

7989
if user_operating_system_str not in OperatingSystem.__members__:
80-
sys.exit("Error: Operating system should be written in all caps from given options.")
81-
else: user_operating_system = OperatingSystem[user_operating_system_str]
90+
sys.exit("Error: Please enter a valid operating system from the given options.")
91+
else:
92+
user_operating_system = OperatingSystem[user_operating_system_str]
8293

8394
user = Person(user_name, user_age, user_operating_system)
8495

8596
count_laptops(laptops, laptop_counts)
86-
print(f"\nThe number of available laptops with {user_operating_system.value} is: {laptop_counts[user_operating_system]}")
97+
print(
98+
f"\nThe number of available laptops with {user_operating_system.value} is: {laptop_counts[user_operating_system]}"
99+
)
87100

88-
check_laptop_abundance(user, laptop_counts)
101+
check_laptop_abundance(user, laptop_counts)

0 commit comments

Comments
 (0)