Skip to content

Instantly share code, notes, and snippets.

@wsteitz
Last active March 30, 2016 10:00
Show Gist options
  • Save wsteitz/0905c749b6b811e1df75 to your computer and use it in GitHub Desktop.
Save wsteitz/0905c749b6b811e1df75 to your computer and use it in GitHub Desktop.
airports with the same IATA code active at the same time
from collections import defaultdict
from itertools import tee, izip
import datetime
airports = defaultdict(list)
# read all period from / to for all airports
with open("optd_por_public.csv") as fin:
for line in fin:
items = line.split('^')
iata_code = items[0]
start = items[13] if items[13] != "" else "1900-01-01"
end = items[14] if items[14] != "" else "2999-12-31"
por_type = items[-5]
if 'A' in por_type and iata_code != "ZZZ":
airports[iata_code].append((start, end))
# filter those with more than one entry and sort by start & end date
airports = {k: sorted(v) for k, v in airports.iteritems() if len(v) > 1}
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
def check_overlap(l):
for (s1, e1), (s2, e2) in pairwise(l):
if e1 > s2: return True
return False
# get those with an overlap
airports = {k: v for k, v in airports.iteritems() if check_overlap(v)}
for k, v in airports.iteritems():
print k, " and ".join(["%s to %s" % (s, e) for s, e in v])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment