Skip to main content

Simple python tool to hash dictionaries using both default hash and sha256.

Project description

Pypi project Pypi total project downloads

Simple python tool to hash dictionaries using both default hash and sha256. The library comes with full support for hashing Pandas DataFrame objects, Numba objects and Numpy arrays, but you will need to specify the requirements when installing the package to avoid bloating the installation process.

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.

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 hash with sha256

Obtain a consistent 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)

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 and Pandas objects.

from dict_hash import sha256
from random_dict import random_dict
from random import randint

# Even though the DataFrame is very big...
df = load_a_very_big_dataframe(...)
# an approximated hash is still very fast!
my_hash = sha256(
    df,
    use_approximation=True
)

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) -> str:
        return sha256({
            "a": self._a
        })

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dict_hash-1.1.32.tar.gz (7.0 kB view details)

Uploaded Source

File details

Details for the file dict_hash-1.1.32.tar.gz.

File metadata

  • Download URL: dict_hash-1.1.32.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.9

File hashes

Hashes for dict_hash-1.1.32.tar.gz
Algorithm Hash digest
SHA256 7be40b48dedf3660a05e5958c205ca798e3a13242bdce915a57c27bccb77025a
MD5 9029d193335c7e9a596d354ef618c78a
BLAKE2b-256 846ff1c6327aac62a21109e4488fe13012bb275fd07a930ae59a9cccb2c5c49f

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page