-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
148 lines (133 loc) · 5.43 KB
/
models.py
File metadata and controls
148 lines (133 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from typing import List, Dict, Any # Import necessary type hints
class Airline:
"""
Airline object. Represents a single airline.
Attributes:
iata: the airline's IATA code.
name: name of the airline
"""
def __init__(self, iata: str, name: str):
self.iata = iata
self.name = name
@classmethod
def from_dict(cls, data: Dict[str, str]):
"""
Deserializes the airline from a dictionary (JSON)
Args:
data (Dict[str, str]): the dictionary (JSON)
Returns:
Instance of Airline
"""
return cls(
iata=data['iata'],
name=data['name']
)
def Airline_to_json_string(self):
"""
Serializes the airline into a JSON string.
Returns:
str: the JSON representation of the airline
"""
return f"Airline(iata='{self.iata}', name='{self.name}')"
class Route:
"""
Route object, represents a single flight route (edge).
Attributes:
iata (str): the airport's iata code.
km (int): the distance in kilometers.
min (int): the flight duration in minutes.
Airlines (List[Airline]): a list of airlines that take this route.
"""
def __init__(self, iata: str, km: int, min: int, Airlines: List[Airline]):
self.iata = iata # Destination airport IATA code
self.km = km # Distance in kilometers
self.min = min # Flight duration in minutes
self.Airlines = Airlines # List of Airlines operating this route
@classmethod
# Deserializes a Route from a dictionary.
def from_dict(cls, data: Dict[str, Any]):
"""
Deserializes the route from a dictionary (JSON)
Args:
data (Dict[str, Any]): the dictionary (JSON)
Returns:
An instance of Route.
"""
Airlines = []
for carrier in data['carriers']:
Airlines.append(Airline.from_dict(carrier))
# return the Route object. Example: Route(iata='GLA', km=0, min=0, Airlines=['BA', 'FR'])
return cls(
iata=data['iata'],
km=data['km'],
min=data['min'],
Airlines=Airlines
)
def route_to_json_string(self):
"""
Serializes the route to a JSON string.
Returns:
str: the JSON representation of the route
"""
return (f"Route(iata='{self.iata}', km={self.km}, min={self.min}, "
f"Airlines={[c.iata for c in self.Airlines]})") # Show IATA codes for brevity
class Airport:
"""
Airport object. Represents a single airport (vertex/node).
Attributes:
city_name (str): The full name of the city where this airport is from.
continent (str): The full name of the continent where the airport is located.
country (str): The full name of the country where the airport is located.
country_code (str): The 2 Character ISO 3166-1 name of the country where the airport is located.
display_name (str): The long display name of the airport.
elevation (str): The elevation of the aiport above MSL.
iata (str): The airport's iata code.
icao (str): The airport's icao code.
latitude (str): The latitude of the airport represented in Decimal Degrees as a string.
longitude (str): The longitude of the airport represented in Decimal Degrees as a string.
name (str): The full name of the airport.
routes (List[Route]): a list of routes out of this airport..
"""
def __init__(self, city_name: str, continent: str, country: str, country_code: str,
display_name: str, elevation: int, iata: str, icao: str,
latitude: float, longitude: float, name: str, routes: List[Route]):
self.city_name = city_name
self.continent = continent
self.country = country
self.country_code = country_code
self.display_name = display_name
self.elevation = elevation
self.iata = iata
self.icao = icao
self.latitude = latitude
self.longitude = longitude
self.name = name
self.routes = routes # List of outgoing routes
@classmethod
def from_dict(cls, iata: str, data: Dict[str, Any]):
"""
Deserializes the Airport from a dictionary (JSON)
Args:
data (Dict[str, Any]): the dictionary (JSON)
Returns:
An instance of Airport.
"""
routes = [Route.from_dict(route_data) for route_data in data['routes']]
if data['latitude'] is None or data['longitude'] is None:
return None
return cls(
city_name=data['city_name'],
continent=data['continent'],
country=data['country'],
country_code=data['country_code'],
display_name=data['display_name'],
elevation=data['elevation'],
iata=iata, # Use the key from the JSON as the IATA code
icao=data['icao'],
latitude=float(data['latitude']), # Convert to float
longitude=float(data['longitude']), # Convert to float
name=data['name'],
routes=routes
)
def __repr__(self):
return f"Airport(iata='{self.iata}', name='{self.name}', routes={len(self.routes)})"