def augment_routes_information(routes, airports): default_src_airport = Airports(507, "London Heathrow Airport", "London", "United Kingdom", "LHR", "EGLL", 51.4706, -0.461941, 83, 0, "E", "Europe/London", "airport") default_dst_airport = Airports(3797, "John F Kennedy International Airport", "New York", "United States", "JFK", "KJFK", 40.63980103, -73.77890015, 13, -5, "A", "America/New_York", "airport") price_per_km = 0.1 route_map = {} i = 1 for rt in routes: src_apt = airports.get(rt.src_apt, default_src_airport) dest_apt = airports.get(rt.dest_apt, default_dst_airport) rt.distance_in_km = calculate_distance(src_apt.latitude, src_apt.longitude, dest_apt.latitude, dest_apt.longitude) rt.price = price_per_km * rt.distance_in_km route_map[i] = rt # print(json.dumps(routes[i].__dict__)) i = i + 1 return route_map
class TestAirportMethods(unittest.TestCase): """Testing the methods from airports.py""" #Create some default airports to work with. #Create an airport system (from class Airports) to be used #in the later methods. def setUp(self): self.LAX = Airport('LAX', (200, 20), ['Eugene']) #Cali self.Eugene = Airport('Eugene', (100, 30), ['LAX']) #oregon self.SEA = Airport('Seattle-Tacoma', (20, 50), ['Eugene']) #washington self.system = Airports() #Test the toString method for an Airport object. def test_str(self): target = "Airport LAX is located at (200, 20) and is connected" target += " to the following ariports: \n~Eugene" self.assertEqual((str(self.LAX)), target) #verify that airports have the correct informatioin #concerning name, location, and connected airports. def test_creating_airport(self): #name, location, connects self.assertEqual(self.LAX.name, 'LAX') self.assertEqual(self.Eugene.location, (100, 30)) self.assertEqual(self.SEA.connects, ['Eugene']) #this method tests adding different airports to our #airport system. def test_add_airport(self): self.system.add_airport(self.LAX) self.assertEqual(self.system.airports[0].name, 'LAX') #this method tests the method add_airports which, when #given a list, adds each Airport object in the list to #system.airports. def test_add_airports(self): lst = [self.Eugene, self.SEA] self.system.add_airports(lst) self.system.add_airport(self.LAX) self.assertEqual(self.system.airports[0].name, 'Eugene') self.assertEqual(self.system.airports[1].name, 'Seattle-Tacoma') self.assertEqual(self.system.airports[2].name, 'LAX') #This method tests the get_distance() method. #Note: distance formula: sqrt[ (x2 - x1)^2 + (y2 - y1)^2] def test_get_distance(self): lst = [self.Eugene, self.SEA, self.LAX] self.system.add_airports(lst) self.assertEqual(self.system.get_distance('Eugene', 'LAX'), 100)
from flask import Flask from flask_restful import Resource, Api, reqparse from airports import Airports app = Flask(__name__) api = Api(app) app.config['SECRET_KEY'] = 'e3esacosdncwefn3rif3ufcs' basePath = '/api/v1' #uguale a quello nel file .yaml airports_util = Airports() class AirportNames(Resource): def get(self, iataCode): if len(iataCode) != 3: return None, 400 return_val = airports_util.get_airport_by_iata(iataCode) if return_val is None: return None, 400 else: return {'name': return_val}, 200 def post(self, iataCode): if len(iataCode) != 3: return None, 400 if airports_util.airport_exist(iataCode): return None, 201 parser = reqparse.RequestParser() parser.add_argument('name', location=["json", "form"])
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import logging from flask import Flask from flask import request from airports import Airports app = Flask(__name__) airport_util = Airports() @app.route('/airportName', methods=['GET']) def airportName(): """Given an airport IATA code, return that airport's name.""" iata_code = request.args.get('iataCode') if iata_code is None: return 'No IATA code provided.', 400 maybe_name = airport_util.get_airport_by_iata(iata_code) if maybe_name is None: return 'IATA code not found : %s' % iata_code, 400 return maybe_name, 200 if __name__ == '__main__': app.run(host='127.0.0.1', port=8080, debug=True)
def setUp(self): self.LAX = Airport('LAX', (200, 20), ['Eugene']) #Cali self.Eugene = Airport('Eugene', (100, 30), ['LAX']) #oregon self.SEA = Airport('Seattle-Tacoma', (20, 50), ['Eugene']) #washington self.system = Airports()