def queue_models(models, context):
 MAX_CYCLES = 5
 model_queue = []
 number_remaining_models = len(models)
 allowed_cycles = MAX_CYCLES
 while (number_remaining_models > 0):
  previous_number_remaining_models = number_remaining_models
  model = models.pop(0)
  if check_dependencies(model, model_queue):
   model_class = ModelCode(model=model, context=context)
   model_queue.append(model_class)
  else:
   models.append(model)
  number_remaining_models = len(models)
  if (number_remaining_models == previous_number_remaining_models):
   allowed_cycles -= 1
   if (allowed_cycles <= 0):
    missing_models = [ModelCode(model=m, context=context) for m in models]
    model_queue += missing_models
    models[:] = missing_models
    break
  else:
   allowed_cycles = MAX_CYCLES
 return model_queue

def queue_models ( models , context ) : MAX_CYCLES = 5 model_queue = [ ] number_remaining_models = len ( models ) allowed_cycles = MAX_CYCLES while ( number_remaining_models > 0 ) : previous_number_remaining_models = number_remaining_models model = models . pop ( 0 ) if check_dependencies ( model , model_queue ) : model_class = ModelCode ( model = model , context = context ) model_queue . append ( model_class ) else : models . append ( model ) number_remaining_models = len ( models ) if ( number_remaining_models == previous_number_remaining_models ) : allowed_cycles -= 1 if ( allowed_cycles <= 0 ) : missing_models = [ ModelCode ( model = m , context = context ) for m in models ] model_queue += missing_models models [ : ] = missing_models break else : allowed_cycles = MAX_CYCLES return model_queue
works an an appropriate ordering for the models .