def from_config(cls, config_dict, merge_default=True): """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. This method should not be called via super unless and instance of the class is desired. :param config_dict: JSON compliant dictionary encapsulating a configuration. :type config_dict: dict :param merge_default: Merge the given configuration on top of the default provided by ``get_default_config``. :type merge_default: bool :return: Constructed instance from the provided config. :rtype: ClassificationElementFactory """ if merge_default: config_dict = merge_dict(cls.get_default_config(), config_dict) ce_type, ce_conf = cls_conf_from_config_dict( config_dict, ClassificationElement.get_impls()) return ClassificationElementFactory(ce_type, ce_conf)
def from_config( cls: Type[T], config_dict: Dict, merge_default: bool = True ) -> T: """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. This method should not be called via super unless and instance of the class is desired. :param config_dict: JSON compliant dictionary encapsulating a configuration. :param merge_default: Merge the given configuration on top of the default provided by ``get_default_config``. :return: Constructed instance from the provided config. """ if merge_default: config_dict = merge_dict(cls.get_default_config(), config_dict) de_type, de_conf = cls_conf_from_config_dict( config_dict, DescriptorElement.get_impls() ) return cls(de_type, de_conf)
def test_cls_conf_from_config_impl_label_mismatch() -> None: test_config = { 'type': 'NotAnImpl', 'T1': { 'foo': 256, 'bar': 'Some string value' }, 'T2': { 'child': { 'foo': -1, 'bar': 'some other value' }, 'alpha': 1.0, 'beta': 'euclidean', }, 'NotAnImpl': {} } with pytest.raises(ValueError, match="Implementation type specified as 'NotAnImpl', " "but no plugin implementations are available for " "that type. Available implementation types " r"options: \[.*\]"): cls_conf_from_config_dict(test_config, T_CLASS_SET)
def test_cls_conf_from_config_config_label_mismatch() -> None: test_config = { 'type': 'not-present-label', 'T1': { 'foo': 256, 'bar': 'Some string value' }, 'T2': { 'child': { 'foo': -1, 'bar': 'some other value' }, 'alpha': 1.0, 'beta': 'euclidean', }, 'NotAnImpl': {} } with pytest.raises(ValueError, match="Implementation type specified as 'not-present-" "label', but no configuration block was present " r"for that type\. Available configuration block " r"options: \[.*\]"): cls_conf_from_config_dict(test_config, T_CLASS_SET)
def test_cls_conf_from_config_none_type() -> None: """ Test that appropriate exception is raised when `type` key is None valued. """ test_config: Dict[str, Any] = { 'type': None, 'T1': { 'foo': 256, 'bar': 'Some string value' }, 'T2': { 'child': { 'foo': -1, 'bar': 'some other value' }, 'alpha': 1.0, 'beta': 'euclidean', }, 'NotAnImpl': {} } with pytest.raises(ValueError, match=r"No implementation type specified\. " r"Options: \[.*\]"): cls_conf_from_config_dict(test_config, T_CLASS_SET)
def test_cls_conf_from_config_missing_type() -> None: """ Test that ``from_config_dict`` throws an exception when no 'type' key is present. """ test_config = { 'T1': { 'foo': 256, 'bar': 'Some string value' }, 'T2': { 'child': { 'foo': -1, 'bar': 'some other value' }, 'alpha': 1.0, 'beta': 'euclidean', }, 'NotAnImpl': {} } with pytest.raises(ValueError, match="Configuration dictionary given does not have an " r"implementation type specification\."): cls_conf_from_config_dict(test_config, T_CLASS_SET)
def test_cls_conf_from_config_dict() -> None: """ Test that ``cls_conf_from_config_dict`` returns the correct type and sub-configuration requested. """ test_config = { 'type': 'tests.test_configuration.T1', 'tests.test_configuration.T1': { 'foo': 256, 'bar': 'Some string value' }, 'tests.test_configuration.T2': { 'child': { 'foo': -1, 'bar': 'some other value' }, 'alpha': 1.0, 'beta': 'euclidean', }, 'NotAnImpl': {} } cls, cls_conf = cls_conf_from_config_dict(test_config, T_CLASS_SET) assert cls == T1 assert cls_conf == {'foo': 256, 'bar': 'Some string value'} test_config['type'] = 'tests.test_configuration.T2' cls, cls_conf = cls_conf_from_config_dict(test_config, T_CLASS_SET) assert cls == T2 assert cls_conf == { 'child': { 'foo': -1, 'bar': 'some other value' }, 'alpha': 1.0, 'beta': 'euclidean', }