Пример #1
0
    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, config_dict, merge_default=True):
        # Override from Configurable
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        elem_type, elem_conf = cls_conf_from_config_dict(
            config_dict, DetectionElement.get_impls())
        return DetectionElementFactory(elem_type, elem_conf)
Пример #3
0
def test_cls_conf_from_config_impl_label_mismatch():
    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 "
                       "options: \[.*\]"):
        cls_conf_from_config_dict(test_config, T_CLASS_SET)
Пример #4
0
def test_cls_conf_from_config_config_label_mismatch():
    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 "
                       "for that type. Available configuration block "
                       "options: \[.*\]"):
        cls_conf_from_config_dict(test_config, T_CLASS_SET)
Пример #5
0
def test_cls_conf_from_config_none_type():
    """
    Test that appropriate exception is raised when `type` key is None valued.
    """
    test_config = {
        '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="No implementation type specified\. "
                       "Options: \[.*\]"):
        cls_conf_from_config_dict(test_config, T_CLASS_SET)
Пример #6
0
def test_cls_conf_from_config_missing_type():
    """
    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 "
                       "implementation type specification\."):
        cls_conf_from_config_dict(test_config, T_CLASS_SET)
Пример #7
0
def test_cls_conf_from_config_dict():
    """
    Test that ``cls_conf_from_config_dict`` returns the corret type and
    sub-configuration requested.
    """
    test_config = {
        'type': 'T1',
        'T1': {
            'foo': 256,
            'bar': 'Some string value'
        },
        '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'] = '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',
    }