import yaml
from typing import OrderedDict


def ordered_dump(data, stream=None, **kwargs):
    """ Dumps a .yml file, retaining the ordering of the dict
    structure.

    Args:
      data (dict): Data dictionary to be saved to .yml file
      stream (file stream, optional): Reference to pre-existing
        .yml file to write to.
      kwargs (float): Key word arguments to be passed to yaml.dump
    """
    class OrderedDumper(yaml.SafeDumper):
        pass

    def _dict_representer(dumper, data):
        return dumper.represent_mapping(
            yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
            data.items())

    OrderedDumper.add_representer(OrderedDict, _dict_representer)

    yaml.dump(data, stream, OrderedDumper, **kwargs)