def overlay(upper, lower):
    """Return the overlay of `upper` dict onto `lower` dict.

    This operation is similar to `dict.update`, but recurses when it encounters
    a dict/mapping, as to allow nested leaf values in the lower collection
    which are not in the upper collection. Whenever the upper collection has a
    value, its value is used.

    >>> overlay({'a': 0}, {})
    {'a': 0}
    >>> abc = {'a': 0, 'b': 1, 'c': 2}
    >>> abc == overlay({'a': 0, 'c': 2}, {'a': None, 'b': 1})
    True
    >>> result = {' ': None, '_': abc}
    >>> result == overlay(
    ...   {'_': {'a': 0, 'c': 2}, ' ': None},
    ...   {'_': {'a': None, 'b': 1}})
    ...
    True
    >>>
    """
    result = {}
    for key in upper:
        if is_mapping(upper[key]):
            lower_value = lower.get(key, {})
            if not is_mapping(lower_value):
                msg = 'Attempting to overlay a mapping on a non-mapping: {}'
                raise ValueError(msg.format(key))
            result[key] = overlay(upper[key], lower_value)
        else:
            result[key] = upper[key]
    for key in lower:
        if key in result:
            continue
        result[key] = lower[key]
    return result