From 043e69669b78111367ed26c43c0551a38c9a12e3 Mon Sep 17 00:00:00 2001 From: willnoonan <90508630+willnoonan@users.noreply.github.com> Date: Thu, 17 Feb 2022 12:56:36 -0600 Subject: [PATCH 1/3] Update .gitignore Ignore .idea in PyCharm --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index af6bfdc..061461e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ +# PyCharm +.idea .vscode/settings.json pythonenv3.8/ __pycache__ dependencies testing -lookup_route.py \ No newline at end of file +lookup_route.py From d2ed9527dab4b34025bd73f9cd4e9dd9d2146cb7 Mon Sep 17 00:00:00 2001 From: williamnoonan Date: Thu, 17 Feb 2022 13:04:49 -0600 Subject: [PATCH 2/3] PyCharm auto-reformatting for improved readability before making code improvements; added 2 spaces between functions. Minimizing line count is not important. Readability is a priority. --- defAirport.py | 78 +++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/defAirport.py b/defAirport.py index ed64da8..344a70d 100644 --- a/defAirport.py +++ b/defAirport.py @@ -1,41 +1,47 @@ import csv import math + + def add_airport_region(airport_dict): - #Get full region/state name from iso region name - with open('./dependencies/regions.csv', 'r', encoding='utf-8') as regions_csv: - regions_csv = csv.DictReader(filter(lambda row: row[0]!='#', regions_csv)) - for region in regions_csv: - if region['code'] == airport_dict['iso_region']: - airport_dict['region'] = region['name'] - return airport_dict + # Get full region/state name from iso region name + with open('./dependencies/regions.csv', 'r', encoding='utf-8') as regions_csv: + regions_csv = csv.DictReader(filter(lambda row: row[0] != '#', regions_csv)) + for region in regions_csv: + if region['code'] == airport_dict['iso_region']: + airport_dict['region'] = region['name'] + return airport_dict + + def getClosestAirport(latitude, longitude, allowed_types): - from geopy.distance import geodesic - plane = (latitude, longitude) - with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv: - airport_csv_reader = csv.DictReader(filter(lambda row: row[0]!='#', airport_csv)) - for airport in airport_csv_reader: - if airport['type'] in allowed_types: - airport_coord = float(airport['latitude_deg']), float(airport['longitude_deg']) - airport_dist = float((geodesic(plane, airport_coord).mi)) - if "closest_airport_dict" not in locals(): - closest_airport_dict = airport - closest_airport_dist = airport_dist - elif airport_dist < closest_airport_dist: - closest_airport_dict = airport - closest_airport_dist = airport_dist - closest_airport_dict['distance_mi'] = closest_airport_dist - #Convert indent key to icao key as its labeled icao in other places not ident - closest_airport_dict['icao'] = closest_airport_dict.pop('gps_code') - closest_airport_dict = add_airport_region(closest_airport_dict) - return closest_airport_dict + from geopy.distance import geodesic + plane = (latitude, longitude) + with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv: + airport_csv_reader = csv.DictReader(filter(lambda row: row[0] != '#', airport_csv)) + for airport in airport_csv_reader: + if airport['type'] in allowed_types: + airport_coord = float(airport['latitude_deg']), float(airport['longitude_deg']) + airport_dist = float((geodesic(plane, airport_coord).mi)) + if "closest_airport_dict" not in locals(): + closest_airport_dict = airport + closest_airport_dist = airport_dist + elif airport_dist < closest_airport_dist: + closest_airport_dict = airport + closest_airport_dist = airport_dist + closest_airport_dict['distance_mi'] = closest_airport_dist + # Convert indent key to icao key as its labeled icao in other places not ident + closest_airport_dict['icao'] = closest_airport_dict.pop('gps_code') + closest_airport_dict = add_airport_region(closest_airport_dict) + return closest_airport_dict + + def get_airport_by_icao(icao): - with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv: - airport_csv_reader = csv.DictReader(filter(lambda row: row[0]!='#', airport_csv)) - for airport in airport_csv_reader: - if airport['gps_code'] == icao: - matching_airport = airport - #Convert indent key to icao key as its labeled icao in other places not ident - matching_airport['icao'] = matching_airport.pop('gps_code') - break - matching_airport = add_airport_region(matching_airport) - return matching_airport \ No newline at end of file + with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv: + airport_csv_reader = csv.DictReader(filter(lambda row: row[0] != '#', airport_csv)) + for airport in airport_csv_reader: + if airport['gps_code'] == icao: + matching_airport = airport + # Convert indent key to icao key as its labeled icao in other places not ident + matching_airport['icao'] = matching_airport.pop('gps_code') + break + matching_airport = add_airport_region(matching_airport) + return matching_airport From c8e4b382b754ffac991b70fe613b1511bbc9ccb1 Mon Sep 17 00:00:00 2001 From: williamnoonan Date: Thu, 17 Feb 2022 13:16:14 -0600 Subject: [PATCH 3/3] Use of 'locals' func unnecessary. --- defAirport.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/defAirport.py b/defAirport.py index 344a70d..073ad46 100644 --- a/defAirport.py +++ b/defAirport.py @@ -15,16 +15,14 @@ def add_airport_region(airport_dict): def getClosestAirport(latitude, longitude, allowed_types): from geopy.distance import geodesic plane = (latitude, longitude) + closest_airport_dict = dict() with open('./dependencies/airports.csv', 'r', encoding='utf-8') as airport_csv: airport_csv_reader = csv.DictReader(filter(lambda row: row[0] != '#', airport_csv)) for airport in airport_csv_reader: if airport['type'] in allowed_types: airport_coord = float(airport['latitude_deg']), float(airport['longitude_deg']) airport_dist = float((geodesic(plane, airport_coord).mi)) - if "closest_airport_dict" not in locals(): - closest_airport_dict = airport - closest_airport_dist = airport_dist - elif airport_dist < closest_airport_dist: + if not closest_airport_dict or airport_dist < closest_airport_dist: closest_airport_dict = airport closest_airport_dist = airport_dist closest_airport_dict['distance_mi'] = closest_airport_dist