Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions app/airports.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@

# Airport data provided by David Megginson (http://ourairports.com/data/).

import collections
import csv
import io

class Airports(object):
"""An interface for reading data about airports."""

def __init__(self):
with open('third_party/airports.csv', 'r') as f:
self.airport_file = io.StringIO(f.read())
self.airport_reader = csv.DictReader(self.airport_file)
with open('third_party/airports.csv', 'rb') as f:
self.airport_reader = csv.DictReader(f)
self.airports = collections.defaultdict(collections.OrderedDict)
for row in self.airport_reader:
self.airports[row['iata_code']] = row

def get_airport_by_iata(self, iata_code):
"""Given an IATA code, look up that airport's name.
Expand All @@ -35,8 +37,6 @@ def get_airport_by_iata(self, iata_code):
string: The airport name, if found.
None: The airport was not found.
"""
self.airport_file.seek(0)
for row in self.airport_reader:
if row['iata_code'] == iata_code:
return row['name']
if iata_code in self.airports:
return self.airports[iata_code]['name']
return None