API reference¶
-
dicthash.
generate_hash_from_dict
(d: dict, blacklist: Optional[List[Hashable]] = None, whitelist: Optional[List[Hashable]] = None, raw: bool = False) → str[source]¶ Generate an md5 hash from a (nested) dictionary.
Takes care of extracting nested dictionaries, iterables and avoids rounding errors of floats. Makes sure keys are read in a unique order. A blacklist of keys can be passed, that can contain keys which should be excluded from the hash. If a whitelist is given, only keys appearing in the whitelist are used to generate the hash. All strings are converted to unicode, i.e., the hash does not distinguish between strings provided in ascii or unicode format. Lists, np.ndarrays and tuples are treated equally, i.e., an array-like item [1,2,3], np.array([1,2,3]) or (1,2,3) will lead to the same hash if they are of the same type.
- d : dict
- Dictionary to compute the hash from.
- blacklist : List[Hashable], optional
- List of keys which are not used for generating the hash. Keys of subdirectories can be provided by specifying the full path of keys in a tuple. If None, no keys will be ignored.
- whitelist : List[Hashable], optional
- List of keys which are used for generating the hash. Keys of subdirectories can be provided by specifying the full path of keys in a tuple. If None, all keys will be used. Blacklist overrules whitelist, i.e., keys appearing in the blacklist will definitely not be used.
- raw : bool, optional
- if True, return the unhashed string.
- str
- The hash generated from the dictionary, or the unhashed string if raw is True.
>>> from dicthash import generate_hash_from_dict >>> d = {'a': 'asd', 'b': 0.12, 3: {'c': [3, 4, 5]}} >>> generate_hash_from_dict(d) 'd748bbf148db514911ed0bf215729d01'