query = """SELECT t.airline, t.flightnum, t.std, c.number_of_chutes, c.opening_before_std, c.closing_before_std FROM cost c, traffic t WHERE c.airline = t.airline AND c.flightnum = t.flightnum ORDER BY t.airline, t.flightnum""" #logging.info(query) cur.execute(query) unallocated = [] # iterates through the first part of departures for airline, flightnum, num_chutes, std, open_before_std, close_before_std in cur.fetchall(): allocations = 0 has_p_chutes = False has_r_chutes = False h = std.hour m = std.minute t = h * 12 + m / 5 print "time: %s: %s is %s" %(std.hour, std.minute, t) # If possible, place departure at a preferred chute ## SELECT all preferred chutes for a given departure: query = """SELECT chute FROM preferred_chute WHERE airline = '%s' AND flightnum = %s""" % (airline, flightnum) cur.execute(query) # iterate through preferred chutes (if any) for the given departure for p_chute in cur.fetchall(): has_p_chutes = True for i in range(len(chutes)): # Is the current chute a preferred one? if (allocations < num_chutes) and schedule[0][i] == p_chute[0]: # Is the chute available during the SLA-time? available = True for j in range(2,5): if not schedule[j][i] == 0 : available = False if available: for j in range(2,5): schedule[j][i] = (airline, flightnum) allocations += 1 if (allocations < num_chutes): # Then place departure at a req chute # Select all required chutes: query = """SELECT chute FROM required_chute WHERE airline = '%s' AND flightnum = %s""" % (airline, flightnum) cur.execute(query) # iterate through the req chutes: for r_chute in cur.fetchall(): has_r_chutes = True for i in range(len(chutes)): # Is the current chute a preferred one? if (allocations < num_chutes) and schedule[0][i] == r_chute[0]: # Is the chute available during the SLA-time? available = True for j in range(2,5): if not schedule[j][i] == 0 : available = False if available: for j in range(2,5): schedule[j][i] = (airline, flightnum) allocations += 1 if not has_r_chutes: # This should never happen! print "Fatal error departure (%s, %s) does not have any required chutes" % (airline, flightnum) sys.exit(1) if (allocations < num_chutes): # Then store the departure as not satisfactorily allocated unallocated.append( (airline, flightnum) )