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+
45+ # ------------------------
46+ # Exercise 1
47+ # ------------------------
48+ # Try changing the type annotation of Person.preferred_operating_system from str to List[str].
49+ # Run mypy on the code.
50+ # It tells us different places that our code is now wrong, because we’re passing values of the wrong type.
51+ # We probably also want to rename our field - lists are plural. Rename the field to preferred_operating_systems.
52+ # Run mypy again.
53+ # Fix all of the places that mypy tells you need changing. Make sure the program works as you’d expect.
54+
55+ @dataclass (frozen = True )
56+ class Person :
57+ name : str
58+ age : int
59+ preferred_operating_systems : List [str ]
60+
61+
62+ @dataclass (frozen = True )
63+ class Laptop :
64+ id : int
65+ manufacturer : str
66+ model : str
67+ screen_size_in_inches : float
68+ operating_system : str
69+
70+ # avoid variable shadowing my changing the internal variable names so that they don't confuse the reader
71+ def find_possible_laptops (available_laptops : List [Laptop ], current_person : Person ) -> List [Laptop ]:
72+ matching_laptops = []
73+ for laptop in available_laptops :
74+ if laptop .operating_system in current_person .preferred_operating_systems :
75+ matching_laptops .append (laptop )
76+ return matching_laptops
77+
78+
79+ people = [
80+ Person (name = "Imran" , age = 22 , preferred_operating_systems = ["Ubuntu" ]),
81+ Person (name = "Eliza" , age = 34 , preferred_operating_systems = ["Arch Linux" ]),
82+ ]
83+
84+ laptops = [
85+ Laptop (id = 1 , manufacturer = "Dell" , model = "XPS" , screen_size_in_inches = 13 , operating_system = "Arch Linux" ),
86+ Laptop (id = 2 , manufacturer = "Dell" , model = "XPS" , screen_size_in_inches = 15 , operating_system = "Ubuntu" ),
87+ Laptop (id = 3 , manufacturer = "Dell" , model = "XPS" , screen_size_in_inches = 15 , operating_system = "ubuntu" ),
88+ Laptop (id = 4 , manufacturer = "Apple" , model = "macBook" , screen_size_in_inches = 13 , operating_system = "macOS" ),
89+ ]
90+
91+ for person in people :
92+ possible_laptops = find_possible_laptops (laptops , person )
93+ print (f"Possible laptops for { person .name } : { possible_laptops } " )
0 commit comments