import hashlib
import pprint


def get_dict_hash(d, shorten: bool = True):
    """
    Create string that uniquely identifies the given dict
    :param d:
    :param shorten: if `True`, will return only the first 8 characters of the hash
    :return:
    """
    # pretty print (necessary to keep a consistent order for dictionary entries, otherwise hash varies for same config), then md5 hash and keep first 8 chars
    hash = hashlib.md5(pprint.pformat(d).encode('utf-8')).hexdigest()
    return hash[:8] if shorten else hash