def params(class_): """ Get the allowed attributes of the class (of the instance). Which attributes are allowed is specified by the class and superclasses (of the instance). Returns ------- params : magni.utils.types.ReadOnlyDict The allowed attributes. Notes ----- The allowed attributes of the class (of the given instance) are found by inspecting the class and its base classes for the static variable '_params' and collecting these in a read-only dictionary. """ params = [] while class_ != BaseClass: if hasattr(class_, '_params'): params.extend(list(class_._params.items())) class_ = class_.__base__ return _ReadOnlyDict(params)
def __init__(self, attrs): @_decorate_validation def validate_input(): _generic('attrs', 'mapping') params = self.params for name in attrs.keys(): if name in params.keys(): _generic(('attrs', name), params[name]) validate_input() names = self.params.keys() attrs = {name: value for name, value in attrs.items() if name in names} self._attrs = _ReadOnlyDict(attrs)