From f519e75f9a73a97aaf08c9e737649d5659918c2e Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Sat, 1 Dec 2018 18:44:59 +0100 Subject: [PATCH] example 5 --- sources/05-airports.py | 42 +++++++++++++++++++++++++++++----------- sources/airports.txt | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 sources/airports.txt diff --git a/sources/05-airports.py b/sources/05-airports.py index 335236a..ab19b81 100644 --- a/sources/05-airports.py +++ b/sources/05-airports.py @@ -1,11 +1,31 @@ -airports = { - "ORY": "Orly", - "LAX": "Los Angeles", -} - -print("Where are you going?") -a = input().upper() -if a in airports: - print(airports[a]) -else: - print(a, "not found") +import sys + + +def build_airport_dict(): + result = dict() + stream = open("airports.txt", "r") + contents = stream.read() + lines = contents.splitlines() + for line in lines: + words = line.split(" ", maxsplit=1) + assert len(words) == 2 + code = words[0] + name = words[1].strip() + result[code] = name + return result + + +def main(): + if len(sys.argv) < 2: + sys.exit("Not enough arguments") + + airport_dict = build_airport_dict() + code = sys.argv[1] + if code not in airport_dict: + sys.exit("Code not found") + result = airport_dict[code] + print(result) + + +if __name__ == "__main__": + main() diff --git a/sources/airports.txt b/sources/airports.txt new file mode 100644 index 0000000..9865d08 --- /dev/null +++ b/sources/airports.txt @@ -0,0 +1,44 @@ +CDG Paris-Charles de Gaulle +ORY Paris-Orly +NCE Nice-Côte d'Azur +LYS Lyon-Saint-Exupéry +TLS Toulouse-Blagnac +MRS Marseille-Provence +BSL Bâle-Mulhouse-Fribourg +BOD Bordeaux-Mérignac +NTE Nantes-Atlantique +BVA Paris-Beauvais-Tillé +PTP Guadeloupe - Pôle Caraïbes +RUN La Réunion-R. Garros +FDF Martinique Aimé Césaire +LIL Lille Lesquin +MPL Montpellier-Méditerranée +AJA Ajaccio +BIA Bastia-Poretta +PPT Tahiti-Faaa +SXB Strasbourg +BIQ Biarritz-Pays Basque +BES Brest-Bretagne +FSC Figari Sud Corse +RNS Rennes-St-Jacques +PUF Pau-Pyrénées +NOU Nouméa - La Tontouta +CAY Cayenne-F. Éboué +TLN Toulon-Hyères +LDE Tarbes-Lourdes-Pyrénées +GEA Nouméa Magenta +PGF Perpignan +CCF Carcassonne +CFE Clermont-Ferrand +DZA Dzaoudzi-Pamandzi +GNB Grenoble-Alpes-Isère +CLY Calvi Sainte-Catherine +EGC Bergerac-Dordogne-Périgord +LIG Limoges-Bellegarde +BOB Bora-Bora +ETZ Metz-Nancy-Lorraine +RFP Raiatea +BZR Béziers - Cap d'Agde +SFG Saint-Martin Grand-Case +LRH La Rochelle-Île de Ré +FNI Nîmes-Garons