{"id":15657418,"url":"https://github.com/lucacappelletti94/dict_hash","last_synced_at":"2026-02-22T22:44:48.643Z","repository":{"id":41300828,"uuid":"189548152","full_name":"LucaCappelletti94/dict_hash","owner":"LucaCappelletti94","description":"Python package to hash dictionaries using default hash, md5, sha256 and more.","archived":false,"fork":false,"pushed_at":"2024-10-26T17:35:03.000Z","size":366,"stargazers_count":24,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-30T00:27:37.184Z","etag":null,"topics":["dictionary","hash","hashing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LucaCappelletti94.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"LucaCappelletti94"}},"created_at":"2019-05-31T07:25:01.000Z","updated_at":"2024-10-26T17:35:07.000Z","dependencies_parsed_at":"2024-06-19T23:18:56.263Z","dependency_job_id":"d68c3ac9-b1e5-44e1-a45e-9ec20f7edfa8","html_url":"https://github.com/LucaCappelletti94/dict_hash","commit_stats":{"total_commits":168,"total_committers":3,"mean_commits":56.0,"dds":"0.017857142857142905","last_synced_commit":"f7e8b3b686a28768f885e00aab652afde8e94ca5"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LucaCappelletti94%2Fdict_hash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LucaCappelletti94%2Fdict_hash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LucaCappelletti94%2Fdict_hash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LucaCappelletti94%2Fdict_hash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LucaCappelletti94","download_url":"https://codeload.github.com/LucaCappelletti94/dict_hash/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054194,"owners_count":21039952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dictionary","hash","hashing"],"created_at":"2024-10-03T13:06:48.612Z","updated_at":"2026-02-22T22:44:48.581Z","avatar_url":"https://github.com/LucaCappelletti94.png","language":"Python","readme":"# Dict Hash\n\n[![Pypi project](https://badge.fury.io/py/dict-hash.svg)](https://badge.fury.io/py/dict-hash)\n[![License](https://img.shields.io/github/license/LucaCappelletti94/dict_hash)](https://github.com/LucaCappelletti94/dict_hash/blob/master/LICENSE)\n[![Pypi total project downloads](https://pepy.tech/badge/dict-hash)](https://pepy.tech/badge/dict-hash)\n[![Github Actions](https://github.com/LucaCappelletti94/dict_hash/actions/workflows/python.yml/badge.svg)](https://github.com/LucaCappelletti94/dict_hash/actions/)\n\nPython package to hash dictionaries using both default hash and sha256.\nIt comes with full support for hashing Pandas \u0026 Polars DataFrame/Series objects, Numba objects and Numpy arrays.\nIt supports both objects from Pandas 1.x and 2.x and Numpy 1.x and 2.x.\n\nFurthermore, the library supports objects that can be recursively hashed.\n\nAs we saw this library being used in the wild mostly to create caching libraries and wrappers,\nwe'd like to point you to our library, [Cache decorator](https://github.com/zommiommy/cache_decorator).\n\n## Why can't I just use the default hash function?\n\nIn Python, dictionaries just aren't hashable. This is because they are mutable objects, and as such, they cannot be hashed.\nIf you were to try and run `hash({})`, you would get a `TypeError` exception.\n\n## How do I install this package?\n\nAs usual, just download it using pip:\n\n```shell\npip install dict_hash\n```\n\n## Usage examples\n\nThe package offers two functions: `sha256` to generate constant sha256 hashes and `dict_hash`, to generate hashes using the native `hash` function.\n\n### Session hash with dict_hash\n\nObtain a session hash from the given dictionary.\n\n```python\nfrom dict_hash import dict_hash\nfrom random_dict import random_dict\nfrom random import randint\n\nd = random_dict(randint(1, 10), randint(1, 10))\nmy_hash = dict_hash(d)\n```\n\n### Consistent hashes\n\nObtain a consistent hash from the given dictionary. Supported methods include `md5`, `sha256`, `sha1`, `sha224`, `sha384`, `sha512`, `sha3_512`, `shake_128`, `shake_256`, `sha3_384`, `sha3_256`, `sha3_224`, `blake2s`, `blake2b`, as provided from the `hashlib` library.\n\nFor instance, to obtain a sha256 hash from the given dictionary:\n\n```python\nfrom dict_hash import sha256\nfrom random_dict import random_dict\nfrom random import randint\n\nd = random_dict(randint(1, 10), randint(1, 10))\nmy_hash = sha256(d)\n```\n\nThe methods `shake_128` and `shake_256` expose the length paramater to specify the length of the hash digest.\n\n```python\nfrom dict_hash import shake_128\nfrom random_dict import random_dict\nfrom random import randint\n\nd = random_dict(randint(1, 10), randint(1, 10))\nmy_hash = shake_128(d, hash_length=16)\n```\n\n### Approximated hash\n\nAll of the methods shown offer the `use_approximation` parameter,\nwhich allows you to switch to a more lightweight hashing procedure\nwhere supported, for the various supported objects. This procedure\nwill randomly subsample the provided objects.\n\nCurrently, we support this parameter for NumPy, Polars, and Pandas objects.\n\n```python\nfrom dict_hash import sha256\nfrom random_dict import random_dict\nfrom random import randint\n\nd = random_dict(randint(1, 10), randint(1, 10))\nmy_hash = sha256(d)\n\napproximated_hash = sha256(d, use_approximation=True)\n```\n\n### Behavior on error\n\nIf the hashing function encounters an object that it cannot hash,\nit will by default raise a `NotHashableException` exception. You\ncan choose whether this or other options happen by setting the\n`behavior_on_error` parameter. You can choose between:\n\n- `raise`: Raise a `NotHashableException` exception.\n- `warn`: Print a `NotHashableWarning` and continue hashing, setting the unhashable object to `\"Unhashable object\"` string.\n- `ignore`: Ignore the object and continue hashing, setting the unhashable object to `\"Unhashable object\"` string.\n\n### Recursive objects\n\nIn Python it is possible to have recursive objects, such as a dictionary that contains itself.\nWhen you attempt to hash such an object, the hashing function will raise a `RecursionError` exception,\nwhich you can customize with the `maximal_recursion` parameter, by default equal to `100`. The\n`RecursionError` is most commonly then handled as a `NotHashableException`, and as such you can\nset the `behavior_on_error` parameter to handle it as you see fit.\n\n### Hashable\n\nWhen handling complex objects within the dictionaries, you may need to implement\nthe class Hashable in that object.\n\nHere is an example:\n\n```python\nfrom dict_hash import Hashable, sha256\n\nclass MyHashable(Hashable):\n\n    def __init__(self, a: int):\n        self._a = a\n        self._time = time()\n\n    def consistent_hash(self, use_approximation: bool = False) -\u003e str:\n        # The use approximation would be useful when the object is too large,\n        # while in this example it may be a bit pointless.\n        if use_approximation:\n            return sha256({\n                \"a\": self._a\n            }, use_approximation=True)\n        return sha256({\n            \"a\": self._a\n        })\n```\n\n## License\n\nThis software is distributed under the MIT license.\n","funding_links":["https://github.com/sponsors/LucaCappelletti94"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucacappelletti94%2Fdict_hash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucacappelletti94%2Fdict_hash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucacappelletti94%2Fdict_hash/lists"}