Пример #1
0
def build_scope(name, parameters, values):
    """Build a single scope where every value in values (a python list) is
    bound to a symbol according to the parameters List given.

    If the parameters list contains `:rest foo`, any remaining arguments
    are passed a list in the named parameter.

    """
    varargs = is_variable_arity(parameters)

    if varargs:
        # The only negative slice supported by RPython is -1, so we
        # have to do it twice.
        normal_parameters = parameters.values[:-1][:-1]
    else:
        normal_parameters = parameters.values

    # Ensure we have the right number of arguments:
    check_parameters(name, parameters, List(values))

    scope = Scope({})
    for variable, value in zip(normal_parameters, values):
        scope.set(variable.symbol_name,  value)

    # todoc: varargs on macros
    # todo: consistently use the terms 'parameters' and 'arguments'
    if varargs:
        # Create a Trifle list of any remaining arguments.
        remaining_args = List(values[len(normal_parameters):])

        # Assign it to the variable args symbol.
        scope.set(parameters.values[-1].symbol_name, remaining_args)

    return scope
Пример #2
0
 def report_on_best_params(self, best_params):
   """Report to the log information about the best parameters found.
      Essentially reporting on whether or not any of the parameters
      seem too close to their range limits or if any of them have
      not been changed from their initial values
   """
   class Arguments:
     """A dummy class just to provide the required arguments to
        the parameters.check_parameters method.
     """
     def __init__(self):
       self.tolerance = 0.01
   arguments = Arguments()
   params = self.optimisation.parameters
   failed_results = parameters.check_parameters(params, 
                                                best_params, 
                                                arguments)
   # This is bad, I've basically copy and pasted this from parameters.py
   # what I should do is have a 'report_on_parameters' in parameters.py
   # which also using the logging module.
   if len(failed_results) == 1:
     logging.info ("No parameters were too close to their range limits")
   else:
     for fail_result in failed_results:
       param = fail_result.param
       if not fail_result.value:
         logging.warning (param.name + 
                          " is not in the best params file(s)")
       elif fail_result.unchanged:
         logging.warning (param.name + 
                          " has not changed from the initial value")
       else:  
         logging.warning (param.name + 
                          " is too close to its range limits")
         logging.warning ("   high limit: " + str(param.high))
         logging.warning ("   low  limit: " + str(param.low))
         logging.warning ("   value     : " + str(fail_result.value))
         logging.warning ("   too high  : " + str(fail_result.too_high))
         logging.warning ("   too low   : " + str(fail_result.too_low))