コード例 #1
0
def test_deep_update():
    source = {'hello1': 1}
    overrides = {'hello2': 2}
    deep_update(source, overrides)
    assert source == {'hello1': 1, 'hello2': 2}

    source = {'hello': 'to_override'}
    overrides = {'hello': 'over'}
    deep_update(source, overrides)
    assert source == {'hello': 'over'}

    source = {'hello': {'value': 'to_override', 'no_change': 1}}
    overrides = {'hello': {'value': 'over'}}
    deep_update(source, overrides)
    assert source == {'hello': {'value': 'over', 'no_change': 1}}

    source = {'hello': {'value': 'to_override', 'no_change': 1}}
    overrides = {'hello': {'value': {}}}
    deep_update(source, overrides)
    assert source == {'hello': {'value': {}, 'no_change': 1}}

    source = {'hello': {'value': {}, 'no_change': 1}}
    overrides = {'hello': {'value': 2}}
    deep_update(source, overrides)
    assert source == {'hello': {'value': 2, 'no_change': 1}}
コード例 #2
0
ファイル: fixture.py プロジェクト: rtoussaint/charlatan
    def get_parent_values(self):
        """Return parent values."""
        parent = self.fixture_manager.collection.get(self.inherit_from)
        # Recursive to make sure everything is updated.
        parent.inherit_from_parent()

        for key in CAN_BE_INHERITED:
            children_value = getattr(self, key)
            parent_value = getattr(parent, key)
            new_value = None
            if not children_value:
                # If I don't have a value, then we try from the parent
                # no matter what.
                new_value = copy.deepcopy(parent_value)

            elif isinstance(children_value, _compat.string_types):
                # The children value is something, then it takes
                # precedence no matter what.
                continue

            elif hasattr(children_value, "update"):
                # If it's a dict, then we try inheriting from the
                # parent.
                new_value = copy.deepcopy(parent_value)
                if self.deep_inherit:
                    deep_update(new_value, children_value)
                else:
                    new_value.update(children_value)

            if new_value:
                yield key, new_value
コード例 #3
0
ファイル: fixture.py プロジェクト: genepeng/charlatan
    def get_parent_values(self):
        """Return parent values."""
        parent = self.fixture_manager.collection.get(self.inherit_from)
        # Recursive to make sure everything is updated.
        parent.inherit_from_parent()

        for key in CAN_BE_INHERITED:
            children_value = getattr(self, key)
            parent_value = getattr(parent, key)
            new_value = None
            if not children_value:
                # If I don't have a value, then we try from the parent
                # no matter what.
                new_value = copy.deepcopy(parent_value)

            elif isinstance(children_value, _compat.string_types):
                # The children value is something, then it takes
                # precedence no matter what.
                continue

            elif hasattr(children_value, "update"):
                # If it's a dict, then we try inheriting from the
                # parent.
                new_value = copy.deepcopy(parent_value)
                if self.deep_inherit:
                    deep_update(new_value, children_value)
                else:
                    new_value.update(children_value)

            if new_value:
                yield key, new_value