Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/niklasrosenstein/python-stablehash
Stable hashing of Python data structures spanning invokations and platforms.
https://github.com/niklasrosenstein/python-stablehash
Last synced: 21 days ago
JSON representation
Stable hashing of Python data structures spanning invokations and platforms.
- Host: GitHub
- URL: https://github.com/niklasrosenstein/python-stablehash
- Owner: NiklasRosenstein
- License: other
- Created: 2024-01-29T00:15:59.000Z (10 months ago)
- Default Branch: develop
- Last Pushed: 2024-09-14T22:49:21.000Z (about 2 months ago)
- Last Synced: 2024-10-08T15:41:47.937Z (about 1 month ago)
- Language: Python
- Size: 52.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```python
from stablehash import stablehashassert 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` objects
- `datetime` objects (`datetime`, `date`, `time` and `timedelta`)
- `uuid.UUID`
- Picklable objects (e.g. those that implement `__getstate__()`)
- `type` objects (by their full qualified name)