def test_load_spec_noparams(): spec = { 'name': 'ballet.validation.feature_acceptance.validator.AlwaysAccepter', # noqa } expected_class = AlwaysAccepter cls, params = load_spec(spec) assert cls is expected_class
def test_load_spec_params(): threshold = 0.88 spec = { 'name': 'ballet.validation.feature_acceptance.validator.RandomAccepter', # noqa 'params': { 'threshold': threshold } } expected_class = RandomAccepter cls, params = load_spec(spec) assert cls is expected_class assert params['threshold'] == threshold
def __init__(self, *args, agg='all', specs: List[dict] = []): super().__init__(*args) self._agg = agg self._specs = specs if not self._specs: raise ValueError('Missing list of accepter specs!') self.accepters = [] for spec in self._specs: cls, params = load_spec(spec) self.accepters.append(cls(*args, **params)) if self._agg == 'all': self.agg = all elif self._agg == 'any': self.agg = any else: raise ValueError( f'Unsupported value for parameter agg: {self._agg}')
def _load_validator_class_params(project: Project, config_key: str) -> Callable: """Load validator class according to config_key with optional params At the provided key, the config should show an entry in one of two forms: 1. The fully-qualified class name of the validator (str) 2. A yaml hash with the key `name` mapping to the fully-qualified class name of the validator, and optionally, the key `params` mapping to hash of keyword arguments to be passed to the validator class. If `params` is provided, then they are partially applied to the validator class ``__init__`` method such that calls to create an instance of the validator class have the given params set as keyword arguments. For example, if the yaml file looks like:: foo: bar: validation: feature_accepter: name: baz.qux.MyFeatureAccepter params: key1: value1 Then:: make_validator = _load_validator_class_params(project, 'foo.bar.validation.feature_accepter') would result in the following equivalence:: make_validator(arg) baz.qux.MyFeatureAccepter(arg, key1=value1) """ # noqa E501 spec = project.config.get(config_key) cls, params = load_spec(spec) return func_partial(cls, **params)
def test_load_spec_from_name(caplog): spec = 'ballet.validation.project_structure.validator.ProjectStructureValidator' # noqa expected_class = ProjectStructureValidator cls, params = load_spec(spec) assert cls is expected_class assert isinstance(params, dict)