Skip to main content

Dict Hash

Pypi project License Pypi total project downloads Github 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.

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:

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.

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:

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.

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.

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:

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.

Metadata

Release files for dict-hash 1.3.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dict-hash 1.3.7
File Size Uploaded
dict_hash-1.3.7.tar.gz 16.0 kB Details

Release files / dict_hash-1.3.7.tar.gz

Download URL dict_hash-1.3.7.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f0c66d9756091c7b99933cc7ca76576246c10ab4c8d08780ae3a42b95823c3c6
BLAKE2b-256 checksum
How to use checksums
5b28d2e5b315f3abc3d6284277499222dd4a7039fc7a80bc4558f513550ec9e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.3.7 This release

1 release file

1.3.6

1 release file

1.3.5

1 release file

1.3.4

1 release file

1.3.3

1 release file

1.3.2

1 release file

1.3.1

1 release file

1.3.0

1 release file

1.2.0

1 release file

1.1.37

1 release file

1.1.36

1 release file

1.1.35

1 release file

1.1.33

1 release file

1.1.32

1 release file

1.1.31

1 release file

1.1.30

1 release file

1.1.29

1 release file

1.1.28

1 release file

1.1.27

1 release file

1.1.26

1 release file

1.1.25

1 release file

1.1.24

1 release file

1.1.23

1 release file

1.1.22

1 release file

1.1.21

1 release file

1.1.20

1 release file

1.1.19

1 release file

1.1.18

1 release file

1.1.17

1 release file

1.1.16

1 release file

1.1.15

1 release file

1.1.14

1 release file

1.1.13

1 release file

1.1.12

1 release file

1.1.11

1 release file

1.1.10

1 release file

1.1.9

1 release file

1.1.8

1 release file

1.1.7

1 release file

1.1.6

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page