Ejemplo n.º 1
0
def test_merge():
    """
    merge function.
    """
    from attrdict.merge import merge

    left = {
        'baz': 'qux',
        'mismatch': False,
        'sub': {'alpha': 'beta', 1: 2},
    }
    right = {
        'lorem': 'ipsum',
        'mismatch': True,
        'sub': {'alpha': 'bravo', 3: 4},
    }

    assert_equals(merge({}, {}), {})
    assert_equals(merge(left, {}), left)
    assert_equals(merge({}, right), right)
    assert_equals(
        merge(left, right),
        {
            'baz': 'qux',
            'lorem': 'ipsum',
            'mismatch': True,
            'sub': {'alpha': 'bravo', 1: 2, 3: 4}
        }
    )
Ejemplo n.º 2
0
    def __add__(self, other):
        """
        Add a mapping to this Attr, creating a new, merged Attr.

        other: A mapping.

        NOTE: Addition is not commutative. a + b != b + a.
        """
        if not isinstance(other, Mapping):
            return NotImplemented

        return self._constructor(merge(self, other), self._configuration())
Ejemplo n.º 3
0
    def load(cls, path=''):
        arg_path = Path(path)
        default_config = read_yaml(cls.DEFAULT_PATH)

        custom_config_path = arg_path.is_file() and arg_path or cls.EXTERNAL_PATH.is_file() and \
                             cls.EXTERNAL_PATH or cls.LOCAL_PATH.is_file() and cls.LOCAL_PATH  # noqa: E127

        if not custom_config_path:
            raise ConfigurationNotFound('You missed a local config in config/ directory '
                                        'or did not specified CPG_CONFIG_PATH environment variable')

        custom_config = read_yaml(custom_config_path) or {}

        return cls(merge(default_config, custom_config))
Ejemplo n.º 4
0
def test_merge():
    """
    merge function.
    """
    from attrdict.merge import merge

    left = {
        'baz': 'qux',
        'mismatch': False,
        'sub': {
            'alpha': 'beta',
            1: 2
        },
    }
    right = {
        'lorem': 'ipsum',
        'mismatch': True,
        'sub': {
            'alpha': 'bravo',
            3: 4
        },
    }

    assert_equals(merge({}, {}), {})
    assert_equals(merge(left, {}), left)
    assert_equals(merge({}, right), right)
    assert_equals(
        merge(left, right), {
            'baz': 'qux',
            'lorem': 'ipsum',
            'mismatch': True,
            'sub': {
                'alpha': 'bravo',
                1: 2,
                3: 4
            }
        })