# Dict Hash
[![Pypi project](https://badge.fury.io/py/dict-hash.svg)](https://badge.fury.io/py/dict-hash)
[![License](https://img.shields.io/github/license/LucaCappelletti94/dict_hash)](https://github.com/LucaCappelletti94/dict_hash/blob/master/LICENSE)
[![Pypi total project downloads](https://pepy.tech/badge/dict-hash)](https://pepy.tech/badge/dict-hash)
[![Github Actions](https://github.com/LucaCappelletti94/dict_hash/actions/workflows/python.yml/badge.svg)](https://github.com/LucaCappelletti94/dict_hash/actions/)
Python package to hash dictionaries using both default hash and sha256.
It comes with full support for hashing Pandas & Polars DataFrame/Series objects, Numba objects and Numpy arrays.
It supports both objects from Pandas 1.x and 2.x and Numpy 1.x and 2.x.
Furthermore, the library supports objects that can be recursively hashed.
As we saw this library being used in the wild mostly to create caching libraries and wrappers,
we'd like to point you to our library, [Cache decorator](https://github.com/zommiommy/cache_decorator).
## Why can't I just use the default hash function?
In Python, dictionaries just aren't hashable. This is because they are mutable objects, and as such, they cannot be hashed.
If you were to try and run `hash({})`, you would get a `TypeError` exception.
## How do I install this package?
As usual, just download it using pip:
```shell
pip install dict_hash
```
## Usage examples
The package offers two functions: `sha256` to generate constant sha256 hashes and `dict_hash`, to generate hashes using the native `hash` function.
### Session hash with dict_hash
Obtain a session hash from the given dictionary.
```python
from dict_hash import dict_hash
from random_dict import random_dict
from random import randint
d = random_dict(randint(1, 10), randint(1, 10))
my_hash = dict_hash(d)
```
### Consistent hashes
Obtain 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.
For instance, to obtain a sha256 hash from the given dictionary:
```python
from dict_hash import sha256
from random_dict import random_dict
from random import randint
d = random_dict(randint(1, 10), randint(1, 10))
my_hash = sha256(d)
```
The methods `shake_128` and `shake_256` expose the length paramater to specify the length of the hash digest.
```python
from dict_hash import shake_128
from random_dict import random_dict
from random import randint
d = random_dict(randint(1, 10), randint(1, 10))
my_hash = shake_128(d, hash_length=16)
```
### Approximated hash
All of the methods shown offer the `use_approximation` parameter,
which allows you to switch to a more lightweight hashing procedure
where supported, for the various supported objects. This procedure
will randomly subsample the provided objects.
Currently, we support this parameter for NumPy, Polars, and Pandas objects.
```python
from dict_hash import sha256
from random_dict import random_dict
from random import randint
d = random_dict(randint(1, 10), randint(1, 10))
my_hash = sha256(d)
approximated_hash = sha256(d, use_approximation=True)
```
### Behavior on error
If the hashing function encounters an object that it cannot hash,
it will by default raise a `NotHashableException` exception. You
can choose whether this or other options happen by setting the
`behavior_on_error` parameter. You can choose between:
- `raise`: Raise a `NotHashableException` exception.
- `warn`: Print a `NotHashableWarning` and continue hashing, setting the unhashable object to `"Unhashable object"` string.
- `ignore`: Ignore the object and continue hashing, setting the unhashable object to `"Unhashable object"` string.
### Recursive objects
In Python it is possible to have recursive objects, such as a dictionary that contains itself.
When you attempt to hash such an object, the hashing function will raise a `RecursionError` exception,
which you can customize with the `maximal_recursion` parameter, by default equal to `100`. The
`RecursionError` is most commonly then handled as a `NotHashableException`, and as such you can
set the `behavior_on_error` parameter to handle it as you see fit.
### Hashable
When handling complex objects within the dictionaries, you may need to implement
the class Hashable in that object.
Here is an example:
```python
from dict_hash import Hashable, sha256
class MyHashable(Hashable):
def __init__(self, a: int):
self._a = a
self._time = time()
def consistent_hash(self, use_approximation: bool = False) -> str:
# The use approximation would be useful when the object is too large,
# while in this example it may be a bit pointless.
if use_approximation:
return sha256({
"a": self._a
}, use_approximation=True)
return sha256({
"a": self._a
})
```
## License
This software is distributed under the MIT license.
Raw data
{
"_id": null,
"home_page": "https://github.com/LucaCappelletti94/dict_hash",
"name": "dict-hash",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Luca Cappelletti",
"author_email": "cappelletti.luca94@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/31/a8/20be4c9d0f9add4fc000893d44f74f85098ac1bd0a3a41bc2162546a2801/dict_hash-1.3.5.tar.gz",
"platform": null,
"description": "# 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 & 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) -> 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\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python package to hash dictionaries using default hash, md5, sha256 and more.",
"version": "1.3.5",
"project_urls": {
"Homepage": "https://github.com/LucaCappelletti94/dict_hash"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "31a820be4c9d0f9add4fc000893d44f74f85098ac1bd0a3a41bc2162546a2801",
"md5": "1c281efc1b3f7a54a40076e1d3f90536",
"sha256": "32614b913c220100f0e06d44130bd2d96a24de5abbf5189ae3180978975f91a3"
},
"downloads": -1,
"filename": "dict_hash-1.3.5.tar.gz",
"has_sig": false,
"md5_digest": "1c281efc1b3f7a54a40076e1d3f90536",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12124,
"upload_time": "2024-10-26T17:34:40",
"upload_time_iso_8601": "2024-10-26T17:34:40.368282Z",
"url": "https://files.pythonhosted.org/packages/31/a8/20be4c9d0f9add4fc000893d44f74f85098ac1bd0a3a41bc2162546a2801/dict_hash-1.3.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-26 17:34:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "LucaCappelletti94",
"github_project": "dict_hash",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "dict-hash"
}