Stable hashing of Python data structures across separate processes and platforms.
Project description
stablehash
The stablehash
module provides a "pure" hash function that is stable across Python processes and runs. This is in
contrast to the builtin hash()
function, which may return a different value for the same input in separate
invokations even with the Python version.
We support most Python built-in types, including mutable types such as list
and dict
, as well as dataclasses. The
default internal hash algorithm is Blake2b, but this can be changed by passing a different hashlib
algorithm to the
stablehash
function.
Usage
from stablehash import stablehash
assert stablehash({"key": "value"}, algorithm="md5").hexdigest() == 'd5994850379366e314563ea555532052'
API
stablehash(obj=..., *, algorithm="blake2b")
Returns a hashlib
-compatible object with the given algorithm and the hash of the given object. The algorithm must be
one of the algorithms supported by hashlib
.
stablehash.update(obj)
Updates the hash with the given object. If the object is not supported, a TypeError
is raised.
stablehash.digest()
Returns the digest of the hash as a bytes object.
stablehash.hexdigest()
Returns the digest of the hash as a string object.
Supported types
The following types are supported:
None
bool
int
float
str
bytes
tuple
list
set
frozenset
dict
@dataclass
objectsdatetime
objects (datetime
,date
,time
andtimedelta
)uuid.UUID
- Picklable objects (e.g. those that implement
__getstate__()
) type
objects (by their full qualified name)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file stablehash-0.2.1.tar.gz
.
File metadata
- Download URL: stablehash-0.2.1.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b547e34cab6f24c199cda76f637a98973b24fb8ddd98f95e509581ed85b429b1 |
|
MD5 | fed8410fb39a492b346de29bdb7ed3aa |
|
BLAKE2b-256 | 0a9f114d42a5d67387aea869f853e72e04479b85809ab5483b6f1e34e82caeb6 |
File details
Details for the file stablehash-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: stablehash-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3605682ba0a4fada6b4436f6f231130d9682acec53b5f9fdbab8d7d8dde516d7 |
|
MD5 | 8f1944027b53e9f2dc9855a19499ccca |
|
BLAKE2b-256 | bc2df0af155f6ca8ee525cdd991d25208b62b5e45a2055eb8cc5f015c681cdc8 |