Context manager for deferring writes of a :py:class:`Bucket` within a
    block.

    Parameters:
        bucket (:py:class:`Bucket`): Bucket to defer writes for within context.

    Returns:
        Bucket to use within context.

    :rtype: :py:class:`DeferredWriteBucket`

    When the context is closed, the stored objects are written to file. The
    in-memory cache of objects is used to update that of the original bucket.

    .. code-block:: python

       bucket = Bucket(path)

       with deferred_write(bucket) as deferred:
           deferred[key] = value
           ...

def deferred_write(bucket):
    """Context manager for deferring writes of a :py:class:`Bucket` within a
    block.

    Parameters:
        bucket (:py:class:`Bucket`): Bucket to defer writes for within context.

    Returns:
        Bucket to use within context.

    :rtype: :py:class:`DeferredWriteBucket`

    When the context is closed, the stored objects are written to file. The
    in-memory cache of objects is used to update that of the original bucket.

    .. code-block:: python

       bucket = Bucket(path)

       with deferred_write(bucket) as deferred:
           deferred[key] = value
           ...
    """
    deferred_write_bucket = DeferredWriteBucket.from_bucket(bucket)
    yield deferred_write_bucket
    deferred_write_bucket.sync()
    bucket._cache.update(deferred_write_bucket._cache)