1from dataclasses import dataclass
2from enum import Enum
3from typing import List
4
5
6class ParkingSpotStatus(str, Enum):
7 FREE = "FREE"
8 OCCUPIED = "OCCUPIED"
9
10
11@dataclass
12class ParkingSpot:
13 number: int
14 status: ParkingSpotStatus
15
16
17# With for and if
18@dataclass
19class Garage:
20 parking_spots: List[ParkingSpot]
21
22 def is_full(self):
23 full = True
24 for spot in self.parking_spots:
25 if spot.status == ParkingSpotStatus.FREE:
26 full = False
27 break
28 return full
29
30
31garage = Garage(parking_spots=[ParkingSpot(number=1, status=ParkingSpotStatus.OCCUPIED)])
32print(garage.is_full())
33
34
35# With all
36@dataclass
37class Garage:
38 parking_spots: List[ParkingSpot]
39
40 def is_full(self):
41 return all(spot.status == ParkingSpotStatus.OCCUPIED for spot in self.parking_spots)
42
43
44garage = Garage(parking_spots=[ParkingSpot(number=1, status=ParkingSpotStatus.OCCUPIED)])
45print(garage.is_full())
46
1# Here all the iterables are True so all
2# will return True and the same will be printed
3print (all([True, True, True, True]))
4
5# Here the method will short-circuit at the
6# first item (False) and will return False.
7print (all([False, True, True, False]))
8
9# This statement will return False, as no
10# True is found in the iterables
11print (all([False, False, False]))
12