def create_instance(self, family_name: str, instance_name_or_spec: Union[dict, str]): dtu.check_isinstance(instance_name_or_spec, (dict, str)) family = self.get_family(family_name) if not family.valid: msg = f"Cannot instantiate {instance_name_or_spec!r} because its family {family_name!r} is " \ f"invalid." msg += "\n\n" + dtu.indent(family.error_if_invalid, " > ") raise dtu.DTConfigException(msg) if isinstance(instance_name_or_spec, str): instance_name = instance_name_or_spec dtu.check_is_in("instance", instance_name, family.instances) instance = family.instances[instance_name] if not instance.valid: msg = f"Cannot instantiate {instance_name!r} because it is invalid:\n" \ f"{dtu.indent(instance.error_if_invalid, '> ')}" raise dtu.DTConfigException(msg) res = dtu.instantiate(instance.constructor, instance.parameters) elif isinstance(instance_name_or_spec, dict): _name, spec = _parse_inline_spec(instance_name_or_spec) res = dtu.instantiate(spec.constructor, spec.parameters) else: assert False interface = dtu.import_name(family.interface) if not isinstance(res, interface): msg = f"I expected that {instance_name_or_spec!r} would be a {interface.__name__} but it is a " \ f"{type(res).__name__}." raise dtu.DTConfigException(msg) return res
def eval_name(x, name_tuple: Sequence[str]): if not name_tuple: return x else: first = name_tuple[0] rest = name_tuple[1:] dtu.check_is_in("value", first, x, EvaluationError) xx = x[first] return eval_name(xx, rest)
def eval(self, rdb): db_entry = rdb.query_results_one(branch=self.branch, date=self.date, commit=self.commit) dtu.dt_check_isinstance("db_entry", db_entry, ResultDBEntry) # print('Results= %s' % db_entry.__repr__()) results = db_entry.results dtu.check_is_in("analyzer", self.analyzer, results, EvaluationError) logs = results[self.analyzer] dtu.check_is_in("log", self.log, logs, EvaluationError) forlog = logs[self.log] val = eval_name(forlog, self.statistic) return val
def get_family(self, x: str): dtu.check_is_in("family", x, self.family_name2config) return self.family_name2config[x]