def write_stop_list_table(out_file, stop_list_map):
    out_file.write("CREATE TABLE IF NOT EXISTS trip_stops (id INTEGER PRIMARY KEY, blob STRING);\n")
    for stop_list_id, lst in stop_list_map.items():
        box = Box()
        box.add_short(len(lst))

        for stop_id, sequence in lst:
            try:
                box.add_string(stop_id)
                box.add_short(sequence)
            except:
                print((stop_id, sequence))
                raise

        out_file.write("INSERT INTO trip_stops VALUES (%d, %s);\n" % (
            stop_list_id, box.get_blob_string()
        ))
def write_arrivals_table(out_file, arrivals_map):
    out_file.write("CREATE TABLE IF NOT EXISTS arrivals (id INTEGER PRIMARY KEY, blob STRING);\n")
    for arrival_id, lst in arrivals_map.items():
        box = Box()
        box.add_short(len(lst))

        for arrival_seconds, departure_seconds in lst:
            if arrival_seconds % 60 != 0:
                raise Exception("arrival_seconds % 60 != 0")
            if (arrival_seconds / 60) > 0xffff or arrival_seconds < 0:
                raise Exception("arrival_seconds out of range")
            box.add_short(arrival_seconds/60)
            #box.add_int(departure_seconds)

        out_file.write("INSERT INTO arrivals VALUES (%d, %s);\n" % (
            arrival_id, box.get_blob_string()
        ))