-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui_controller.py
More file actions
149 lines (117 loc) · 4.54 KB
/
ui_controller.py
File metadata and controls
149 lines (117 loc) · 4.54 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
149
from airport_network import AirportNetwork
from models import Airport
import travel_data as travel
import flight_ticket_price_api as flight_api
import currency_api
import sorting as sort
import configuration as config
network = AirportNetwork.from_json_file("data/airline_routes.json")
# Dictionary for lookups
airport_dict={}
for iata, airport in network.airports.items():
if airport is not None:
airport_dict[f"{airport.name} ({iata}) - {airport.country} "] = iata
# Search for flights
def search(input_path):
results = {}
num_of_legs = len(input_path)
for i in range(0, num_of_legs):
start_airport = input_path[i][0]
dest_airport = input_path[i][1]
if start_airport == dest_airport:
return None
routes = network.find_routes(start_airport, dest_airport)
if routes:
results[(start_airport, dest_airport)] = routes
return results
# One way flight search
def one_way_flight(start_airport, dest_airport):
if start_airport != dest_airport:
return search([(start_airport, dest_airport)])
else:
return None
# Return flight search
def return_flight(start_airport, dest_airport):
if start_airport != dest_airport:
return search([(start_airport, dest_airport)]), search([(dest_airport, start_airport)])
else:
return None
# Multi city flight search
def multi_city_flight(input_path):
return search(input_path)
# Get live pricing from API
def get_price_from_API(path, date, cabin_class):
return flight_api.get_price_range(path, date, cabin_class)
# Convert currency from SGD to other currencies
def convert_currency(price, currency):
return int(currency_api.convert_currency_from_SGD(price, currency))
# Calculate price for cabin class and number of passengers
def calculate_total_price(price, cabin_class, num_of_pax):
return int(price * config.CABIN_CLASS[cabin_class] * num_of_pax)
def calculate_cabin_price(price, cabin_class):
return int(price * config.CABIN_CLASS[cabin_class])
# Get list of currency
def get_list_of_currency():
return currency_api.get_list_of_currency()
# Get airport name from IATA code
def get_airport_name(iata):
airport = network.get_airport_by_iata(iata)
return airport.name
# Get all airport name
def getAllAirportName():
return airport_dict
# Get lattitude and longitude of airports in the path
def get_lat_long(path):
coord_data = []
for i in range(0, len(path)):
airport = network.get_airport_by_iata(path[i])
coord_data.append((airport.latitude, airport.longitude))
return coord_data
# Sort the results
def get_most_convenient_route(results):
return sort.quicksort_dict(results, 0)
def get_shortest_route(results):
return sort.quicksort_dict(results, 1)
def get_least_stops(results):
return sort.quicksort_dict(results, 3)
def get_least_duration(results):
return sort.quicksort_dict(results, 4)
def get_least_emission(results):
return sort.quicksort_dict(results, 5)
def get_cheapest_price(results):
return sort.quicksort_dict(results, 6)
# Filter the results for given filter type
def filter(results, filter):
filter_functions= {
"Recommended": get_most_convenient_route,
"Shortest Distance": get_shortest_route,
"Fewest Stops": get_least_stops,
"Cheapest Flight": get_cheapest_price,
"Lowest CO2 Emissions": get_least_emission,
"Least Duration": get_least_duration
}
filter_func = filter_functions.get(filter)
if filter_func == None:
return results
else:
return filter_func(results)
def getTravelerData(iata):
return travel.get_average_accommodation(network.airports[iata].country)
def getTravelerDataCountry(country):
return travel.get_average_accommodation(country)
if __name__ == "__main__":
"""
Unit test for the ui_controller. Can be run by running this particular python file.
"""
#a,b,c,d =travel.get_average_accommodation("Brazil")
#rint(f'Avg accoms: {a} Avg Transport:{b} Avg days:{c} Most Accoms Type:{d}')
#result = search([('SIN', 'GLA'), ('SIN', 'PNH')])
#print(get_cheapest_price(result))
#print(get_least_duration(result))
#print(get_least_emission(result))
#print(get_lat_long(['SIN', 'GLA', 'JFK']))
#print(filter_list_by_airport('AMS', result))
#print(filter_list_by_stops(1, search([('SIN', 'GLA')])))
#print(get_price_from_API(['SIN', 'CDG'], '2025-12-12', 'Economy'))
#expected output:
# { Route: [(penalty, distance, path, stops, duration, emission, price)] }