Skip to content

Commit 451130c

Browse files
author
l
committed
refactoring
1 parent 259c684 commit 451130c

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

‎sprint-5/12-refactor.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_system: 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 == person.preferred_operating_system:
24+
possible_laptops.append(laptop)
25+
return possible_laptops
26+
27+
28+
people = [
29+
Person(name="Imran", age=22, preferred_operating_system="Ubuntu"),
30+
Person(name="Eliza", age=34, preferred_operating_system="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}")
43+
44+
# Task 12
45+
#Try changing the type annotation of `Person.preferred_operating_system` from `str` to `List[str]`.
46+
#
47+
#Run mypy on the code.
48+
#
49+
#It tells us different places that our code is now wrong. Fix it to remov eany errors.
50+
#
51+
#Now we changed the types, we probably also want to _rename_ our fields to something appropriate.
52+
#
53+
#Run mypy again.
54+
#
55+
#Fix all of the places that mypy tells you need changing.
56+
#
57+
#Then, make sure the program works as you'd expect.

0 commit comments

Comments
 (0)