flight_functions.py
CSCA08: Functions for Assignment 3 - OpenFlights
from typing import Dict, List, Set, Tuple
from flight_types_constants_and_test_data import (
AIRPORT_DATA_INDEXES, ROUTE_DATA INDEXES, FLIGHT_DATA_INDEXES,
AirportDict, RouteDict, FlightDir,
)
def get_airport_info(airports: AirportDict, iata: str, info: str) -> str:
“”” Return the airport information for airport with IATA code iata for column info from AIRPORT DATA INDEXES.
*>> get_airport_info(TEST_AIRPORTS_DICT, 'AA1’, 'Name")
‘Apt1’
› get_airport_info(TEST_AIRPORTS_DICT, 'AA4', "IATA')
‘AA4’
“””
# Complete the function body
def is _direct_flight(iata _src: str, iata_dst: str, routes: RouteDict) -› bool:
“””Return whether there is a direct flight from the iata_src airport to the iata_dst airport in the routes dictionary. iata_src may not be a key in the routes dictionary.
>>> is_direct_flight (‘AA1', 'AA2", TEST_ROUTES_DICT_FOUR_CITIES)
True
>>> is direct_flight ('AA2’, ‘AA1, TEST_ROUTES DICT_FOUR _CITIES)
False
“””
# Complete the function body
def is_valid_flight sequence(iata_list: List[str], routes: RouteDict) -> bool:
" Return whether there are flights from iata_list [i]to iata_list[i + 1] for all valid values of i. IATA entries may not appear anywhere in routes.
>>> is valid flight_ sequence ( ['AA3’, 'AA1’, ‘AA2’,], TEST_ROUTES_DICT_FOUR_CITIES)
True
>>> is valid flight_ sequence (['AA3’, 'AA1',
AA2', 'AA1’, ‘AA2’], TEST_ROUTES_DICT _FOUR_CITIES)
False
“””
# Complete the function body
def count_outgoing_flights ():
# Complete this function (including type contract and docstring)
# based on the handout description
pass
def count_ incoming_flights():
# Complete this function (including type contract and docstring)
# based on the handout description
pass
def count_total_flights():
# Complete this function (including type contract and docstring)
# based on the handout description
pass
def reachable_destinations (iata_ src: str, n: int, routes: RouteDict) -> List[Set[str]]:
“””Return a list of the sets of airports where the set at index i is reachable in at most i flights. Note that iata_src will always appear at index 0, because it is reachable without flying anywhere
Precondition: n >= 0
>>>reachable_destinations ('AA1', 0, TEST_ ROUTES_DICT_FOUR_CITIES)
[{‘AA1’}]
>>>expected = [{‘AA1’},{‘AA2’, ‘AA4’}]
>>>result = reachable_destinations (‘AA1’, 1, TEST_ROUTES_DICT_FOUR_CITIES)
>>> expected == result
True
“””
# Complete your function below
def calculate_trip_time(iata_src: str, iata_dst: str, flight_walk: List[str], flights: FlightDir) -> float:
“””Return a float corresponding to the amount of time required to travel from the source airport to the destination airport to the destination airport, as outlined by the flight_walk.
The start time of the trip should be considered zero. In other words, assuming we start the trip at 12:00am, this function should return the time it takes for the trip to finish, including all the waiting times before, and between the flights.
If there is no path available return -1.0
>>> calculate_trip _time ("AAl", "AA2", ["'AAl", "AA2"], TEST_FLIGHTS DIR_FOUR_CITIES)
2.0
>>> calculate_trip_time("AAl", "AA7", ["AA7", "AAl"], TEST_FLIGHTS DIR_FOUR _CITIES)
1.0
>>> calculate_trip_time ("AAl", "AA7", ["AAl", "AA7"1, TEST _FLIGHTS _DIR FOUR_ CITIES)
-1.0
>>> calculate _trip_time ("AAl", "AAl", ["AAl"], TEST_FLIGHTS_DIR_FOUR_CITIES)
0.0
>>> calculate trip _time ("AA4", "AA2", ["AA4", "AA1", "AA2"], TEST_FLIGHTS_DIR_FOUR_CITIES)
14.0
>>> calculate _trip_time ("AAI", "AA3", ["AAl", "AA2", "AA3"], TEST_FLIGHTS_DIR_FOUR_CITIES)
7.5
>>> calculate_trip_time ("AAl", "AA4", ["AAl", "AA4"], TEST_FLIGHTS_DIR_FOUR_CITIES)
2.0
# Complete your function below
if __name__ == “__main__”:
“””Uncommment the following as needed to run your doctests"""
# from flight_ types_constants_and test_data import TEST_AIRPORTS_DICT
# from flight_types_constants_and_test_data import TEST_AIRPORTS_SRC
# from flight_types_constants_and_test_data import TEST_ROUTES_DICT_FOUR_CITIES
# from flight_types_constants_and_ test_data import TEST_ROUTES_SRC
# from flight_ types_constants_and test_data import TEST_FLIGHTS_DIR_FOUR_CITIES
# from flight types constants and test data import TEST FLIGHTS SC
# import doctest
# doctest.testmod ( )
flight_reader.py
“””Functions for CSCA08 Assignment 3 to read in the airport and route data.
“””
from typing import TextIO
# Note about "from io import StringIO" in the docstrings:
# We can use StringIO to pretend that a string is the contents of a file.
# We are only using it for the examples below, to help you understand what
# the functions will do.
# You do NOT have to use it yourself.
from flight_types_constants_and_test_data import (
AIRPORT_DATA_INDEXES, ROUTE_DATA INDEXES, FLIGHT_DATA_INDEXES,
AirportDict, RouteDict, FlightDir,
)
def is_number (value: str) -> bool:
"””Return True if and only if represents a number
>>>is_number ('YYZ')
False
>>> is _number ('3.0')
True
return value.strip().lstrip("+-").replace(".”, “”, 1).isnumeric()
def read_airports(airports_source: TextI0) -> AirportDict:
"”''Return a dictionary containing the information in airports_source.
Skip entries that have no IATA code.
>>> from io import StringIO
>>> airports_src = StringIO(TEST_AIRPORTS_SRC)
>>> airports_res = read_airports(airports_src)
» airports_res['AA1'][0], airports_res[ ‘AA1'][1]
(‘1','Apt1')
››› airports_res['AA4'][0], airports_res ['AA4'][1]
('4', 'Apt4')
›››len(airports_res)
4
>>> airports_res == TEST_AIRPORTS_DICT
True
“””
# Complete this function.
def read _routes (routes_source: TextIO, airports: AirportDict) -> RouteDict:
“””Return the flight routes from routes_source, including only the ones that have an entry in airports. If there are multiple routes between routes_source and a destination (on different airlines for example), include the destination only once. Routes that include null airport IDs should still be included, but routes that have empty IATA should be excluded.
>>>from io import StringIO
>>> routes_src = StringIO(TEST_ROUTES_SRC)
>>>actual = read_routes (routes_src, TEST _AIRPORTS_DICT)
>>> actual==TEST_ROUTES_DICT_FOUR_CITIES
True
“””
routes = {}
src_ index = ROUTE_DATA_INDEXES["Source airport"]
dst index = ROUTE_DATA_INDEXES["Destination airport"]
# Complete this function.
# Note that each value in the resulting dictionary is a set of IATA codes.
def read_flights(flights_source: TextIO, routes: RouteDict) -> FlightDir:
''''Return the flights from flights_ source, including only the ones that have an entry in routes
>>> from io import StringIO
>>> flight_src = StringIO(TEST_FLIGHTS_SRC)
>>>actual = read_flights (flight_src, TEST_ROUTES _DICT_FOUR_CITIES)
>>>actual == TEST_FLIGHTS_DIR_FOUR_CITIES
True
“””
flights = [ ]
src_index = FLIGHT_DATA_INDEXES["Source airport"]
dst_index = FLIGHT_DATA_INDEXES[ "Destination airport"1
# Complete this function.
if __name__ == “__main__”:
“””Uncommment the following as needed to run your doctests"""
# from flight_ types_constants_and test_data import TEST_AIRPORTS_DICT
# from flight_types_constants_and_test_data import TEST_AIRPORTS_SRC
# from flight_types_constants_and_test_data import TEST_ROUTES_DICT_FOUR_CITIES
# from flight_types_constants_and_ test_data import TEST_ROUTES_SRC
# from flight_ types_constants_and test_data import TEST_FLIGHTS_DIR_FOUR_CITIES
# from flight types constants and test data import TEST FLIGHTS SC
# import doctest
# doctest.testmod ( )