def reverse_map(D):
    """
    Return a dict having D.values() as its keys and D.keys() as its values.
    """
    E = {}
    for k, v in D.items():
        if v in E:
            raise KeyError(
                "Cannot reverse this mapping, as it is not one-to-one ('%s' would map to both '%s' and '%s')"
                 % (v, E[v], k))
        E[v] = k
    return E
