def check(self, task): """Test all parameters evaluation. """ traceback = {} i = 0 kind = 'Analogical{}_' try: task.format_and_eval_string(self.active) except Exception: mess = 'Failed to eval active : {}' traceback['Channel'] = mess.format(format_exc()) for p in chain(self.analogicals, self.logicals): if i == self.analogical: kind = 'Logical{}_' for n in tagged_members(p, 'check'): val = getattr(p, n) if not val: continue try: task.format_and_eval_string(val) except Exception: mess = 'Failed to eval {} : {}' traceback[kind.format(i) + n] = mess.format(val, format_exc()) i += 1 return not bool(traceback), traceback
def test_tagged_members3(): aux = _Aux() members = sorted(tagged_members(aux).keys()) test = sorted([ 'string', 'float_n', 'enum', 'enum_float', 'list_', 'dict_', 'atom', 'no_tag', 'value', 'const' ]) assert members == test
def test_tagged_members3(): aux = _Aux() members = sorted(tagged_members(aux).keys()) test = sorted(['string', 'float_n', 'enum', 'enum_float', 'list_', 'odict_', 'atom', 'no_tag', 'value', 'const']) assert members == test
def test_tagged_members2(): aux = _Aux() members = tagged_members(aux, 'pref', [ordered_dict_to_pref, ordered_dict_from_pref]) assert list(members) == ['odict_']
def test_tagged_members2(): aux = _Aux() members = tagged_members(aux, 'pref', False).keys() assert members == ['float_n']
def test_tagged_members3(): aux = _Aux() members = sorted(tagged_members(aux).keys()) test = sorted(["string", "float_n", "enum", "enum_float", "list_", "dict_", "atom", "no_tag", "value", "const"]) assert members == test
def test_tagged_members2(): aux = _Aux() members = tagged_members(aux, "pref", False).keys() assert members == ["float_n"]
def eval_entries(self, global_vars, local_vars, missings, errors): """Evaluate and format all tagged members. The result of the evaluation is written to the _cache dictionary. Parameters ---------- global_vars : dict Dictionary of global variables. This will be update will the valid values whose tag specify they should be stored as global. local_vars : dict Dictionary of variables used for evaluation. This will be update will the valid values whose tag specify they should be stored as global. missings : set Set of unfound local variables. errors : dict Dict of the errors which happened when performing the evaluation. Returns ------- flag : bool Boolean indicating whether or not the evaluation succeeded. """ res = True cache = self._cache for member, store in tagged_members(self, 'fmt').items(): if member in cache: continue fmt_str = getattr(self, member) try: fmt = fmt_str.format(**local_vars) self._cache[member] = fmt if store: id_ = self.format_global_vars_id(member) global_vars[id_] = fmt local_vars[id_] = fmt # This can only occur if a {} field was found and an entry is # missing except KeyError: res = False aux_strings = fmt_str.split('{') elements = [el for aux in aux_strings for el in aux.split('}')] absent = [el for el in elements[1::2] if el not in local_vars] missings.update(absent) except Exception: res = False errors[self.format_error_id(member)] = format_exc() for member, m in tagged_members(self, 'feval').items(): feval = m.metadata['feval'] if member in cache: continue if not feval.should_test(self, member): continue try: val, store = feval.evaluate(self, member, local_vars) valid, msg = feval.validate(self, val) if not valid: res = False errors[self.format_error_id(member)] = msg else: self._cache[member] = val if store: id_ = self.format_global_vars_id(member) global_vars[id_] = val local_vars[id_] = val except MissingLocalVars as e: res = False missings.update(e.missings) except Exception: res = False errors[self.format_error_id(member)] = format_exc() return res
def eval_entries(self, global_vars, local_vars, missings, errors): """Evaluate and format all tagged members. The result of the evaluation is written to the _cache dictionary. Parameters ---------- global_vars : dict Dictionary of global variables. This will be update will the valid values whose tag specify they should be stored as global. local_vars : dict Dictionary of variables used for evaluation. This will be update will the valid values whose tag specify they should be stored as global. missings : set Set of unfound local variables. errors : dict Dict of the errors which happened when performing the evaluation. Returns ------- flag : bool Boolean indicating whether or not the evaluation succeeded. """ res = True cache = self._cache for member, m in tagged_members(self, 'fmt').items(): if member in cache: continue fmt_str = getattr(self, member) try: fmt = fmt_str.format(**local_vars) self._cache[member] = fmt if m.metadata['fmt']: id_ = self.format_global_vars_id(member) global_vars[id_] = fmt local_vars[id_] = fmt # This can only occur if a {} field was found and an entry is # missing except KeyError: res = False aux_strings = fmt_str.split('{') elements = [el for aux in aux_strings for el in aux.split('}')] absent = [el for el in elements[1::2] if el not in local_vars] missings.update(absent) except Exception: res = False errors[self.format_error_id(member)] = format_exc() for member, m in tagged_members(self, 'feval').items(): feval = m.metadata['feval'] if member in cache: continue if not feval.should_test(self, member): continue try: val, store = feval.evaluate(self, member, local_vars) valid, msg = feval.validate(self, val) if not valid: res = False errors[self.format_error_id(member)] = msg else: self._cache[member] = val if store: id_ = self.format_global_vars_id(member) global_vars[id_] = val local_vars[id_] = val except MissingLocalVars as e: res = False missings.update(e.missings) except Exception: res = False errors[self.format_error_id(member)] = format_exc() return res