def __call__(self, fixed=None, preset=None, fallback=None): """ Evaluate this ConfigScope. This will evaluate the function body and fill the relevant local variables into entries into keys in this dictionary. :param fixed: Dictionary of entries that should stay fixed during the evaluation. All of them will be part of the final config. :type fixed: dict :param preset: Dictionary of preset values that will be available during the evaluation (if they are declared in the function argument list). All of them will be part of the final config. :type preset: dict :param fallback: Dictionary of fallback values that will be available during the evaluation (if they are declared in the function argument list). They will NOT be part of the final config. :type fallback: dict :return: self :rtype: ConfigScope """ cfg_locals = dogmatize(fixed or {}) fallback = fallback or {} preset = preset or {} fallback_view = {} available_entries = set(preset.keys()) | set(fallback.keys()) for arg in self.arg_spec.args: if arg not in available_entries: raise KeyError("'{}' not in preset for ConfigScope. " "Available options are: {}".format( arg, available_entries)) if arg in preset: cfg_locals[arg] = preset[arg] else: # arg in fallback fallback_view[arg] = fallback[arg] cfg_locals.fallback = fallback_view eval(self._body_code, copy(self._func.__globals__), cfg_locals) added = cfg_locals.revelation() config_summary = ConfigSummary(added, cfg_locals.modified, cfg_locals.typechanges, cfg_locals.fallback_writes, docs=self._var_docs) # fill in the unused presets recursive_fill_in(cfg_locals, preset) for key, value in cfg_locals.items(): try: config_summary[key] = normalize_or_die(value) except ValueError: pass return config_summary
def __call__(self, fixed=None, preset=None, fallback=None): """ Evaluate this ConfigScope. This will evaluate the function body and fill the relevant local variables into entries into keys in this dictionary. :param fixed: Dictionary of entries that should stay fixed during the evaluation. All of them will be part of the final config. :type fixed: dict :param preset: Dictionary of preset values that will be available during the evaluation (if they are declared in the function argument list). All of them will be part of the final config. :type preset: dict :param fallback: Dictionary of fallback values that will be available during the evaluation (if they are declared in the function argument list). They will NOT be part of the final config. :type fallback: dict :return: self :rtype: ConfigScope """ cfg_locals = dogmatize(fixed or {}) fallback = fallback or {} preset = preset or {} fallback_view = {} available_entries = set(preset.keys()) | set(fallback.keys()) for arg in self.args: if arg not in available_entries: raise KeyError("'{}' not in preset for ConfigScope. " "Available options are: {}" .format(arg, available_entries)) if arg in preset: cfg_locals[arg] = preset[arg] else: # arg in fallback fallback_view[arg] = fallback[arg] cfg_locals.fallback = fallback_view eval(self._body_code, copy(self._func.__globals__), cfg_locals) added = cfg_locals.revelation() config_summary = ConfigSummary(added, cfg_locals.modified, cfg_locals.typechanges, cfg_locals.fallback_writes, docs=self._var_docs) # fill in the unused presets recursive_fill_in(cfg_locals, preset) for key, value in cfg_locals.items(): try: config_summary[key] = normalize_or_die(value) except ValueError: pass return config_summary
def __init__(self, d): self._conf = normalize_or_die(d)
def test_normalize_or_die_for_numpy_arrays(typename): np = opt.np dtype = getattr(np, typename) a = np.array([0, 1, 2], dtype=dtype) b = normalize_or_die(a) assert len(b) == 3
def test_normalize_or_die_for_numpy_datatypes(typename): dtype = getattr(opt.np, typename) assert normalize_or_die(dtype(7.0))
def __init__(self, d): super(ConfigDict, self).__init__() self._conf = normalize_or_die(d)