Last active
January 29, 2020 16:53
-
-
Save vitor-diego-s/74b15fb26f702cd4bf8f965eeb265558 to your computer and use it in GitHub Desktop.
Good reference for hash creation - based on some kind of data type - https://stackoverflow.com/users/660554/jomido ( Author )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_hash(target): | |
''' | |
Makes a hash from a dictionary, list, tuple or set to any level, that contains | |
only other hashable types (including any lists, tuples, sets, and | |
dictionaries). | |
https://stackoverflow.com/questions/5884066/hashing-a-dictionary | |
''' | |
if isinstance(target, (set, tuple, list)): | |
return tuple([make_hash(e) for e in target]) | |
elif not isinstance(target, dict): | |
return hash(target) | |
new_o = copy.deepcopy(target) | |
for k, v in new_o.items(): | |
new_o[k] = make_hash(v) | |
return hash(tuple(frozenset(sorted(new_o.items())))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment