Skip to main content

dClimate logo

py-hamt

codecov

This is a python implementation of a HAMT, inspired by rvagg's IAMap project written in JavaScript.

py-hamt provides efficient storage and retrieval of large sets of key-value mappings in a content-addressed storage system. The main target is IPFS, and the data model used is IPLD.

dClimate primarily created this for storing large zarrs on IPFS. To see this in action, see our data ETLs.

Installation and Usage

pip install py-hamt

For usage information, take a look at our API documentation, major items have example code.

You can also see this library used in either our data ETLs or Jupyter notebooks for data analysis.

Development Guide

Setting Up

py-hamt uses uv for project management. Make sure you install that first. Once uv is installed, run

uv sync
source .venv/bin/activate
pre-commit install

to create the project virtual environment at .venv.

Then you can run pre-commit across the whole codebase with

pre-commit run --all-files

the below command run-checks.sh in the next section will also run this command inside its bash script.

Run tests, formatting, linting

First, make sure you have the ipfs kubo daemon installed and running with the default endpoints open. Then run the script

bash run-checks.sh

This will run tests with code coverage, and check formatting and linting. Under the hood it will be using the pre-commit command to run through all the checks within .pre-commit-config.yaml. If a local ipfs daemon is not running it will not run all tests, but it will spawn a docker ipfs container if docker is installed and run as many integration tests as possible.

We use pytest with 100% code coverage, and with test inputs that are both handwritten as well as generated by hypothesis. This allows us to try out millions of randomized inputs to create a more robust library.

OpenTelemetry trace detail

Control trace production at the instrumentation source, independently of Python logging levels and aggregate metrics:

Environment variable Values Default
PY_HAMT_OTEL_TRACE_MODE full, diagnostic, off full
PY_HAMT_OTEL_SLOW_SECONDS Finite, nonnegative seconds 5

full preserves the existing nested span for every block load, HAMT lookup and Zarr store read. For busy workers, select diagnostic to create spans only after an operation fails, retries or fails over a CAS read, misses a HAMT lookup, or reaches the slow threshold. Diagnostic spans retain operation attributes, actual start/end times, and the caller's parent trace/job context. They attach directly to that caller. Ordinary successful reads have no individual spans in this mode. Set full on a worker during an investigation when complete low-level history is needed; previously omitted history cannot be recovered.

To protect gateway credentials, full and diagnostic spans export exception types and stack frame locations. For HTTP failures they also record numeric response codes as http.response.status_code for one response or py_hamt.http.response.status_codes for multiple responses. CAS spans also record py_hamt.cas.failed_http_status_codes for failed attempts, including those followed by a successful retry or failover.

off emits no py-hamt operation spans, including diagnostic spans, while leaving the caller's active span intact. Aggregate request, byte, retry and latency metrics remain active in every mode when a metrics provider is configured. Invalid modes or diagnostic thresholds raise ValueError; threshold validation is only relevant in diagnostic mode. Mode and threshold are read when each operation begins, so its decision remains consistent through completion.

For example:

PY_HAMT_OTEL_TRACE_MODE=diagnostic PY_HAMT_OTEL_SLOW_SECONDS=5 python worker.py

PY_HAMT_TRACE and IPFS_RETRIEVAL_TRACE continue to control the separate local diagnostic snapshots. They do not select OpenTelemetry trace detail. The library continues to depend only on the OpenTelemetry API; applications configure their own SDK, exporters and global trace sampler.

Tests

Due to the dependency on IPFS in order to be able to run all integration tests which use IPFS a local ipfs daemon is required. The Github Actions found in .github/workflows/run-checks.yaml uses the setup-ipfs step which ensures that a local ipfs daemon is available. Locally if you wish to run the full integration tests you must ensure a local ipfs daemon is running (by running ipfs daemon once installed). If not, pytest will spawn a local docker image to run the ipfs tests. If Docker is not installed then tests will simply run the unit tests.

To summarize:

In GitHub Actions:

uv run pytest --ipfs  # All tests run, including test_kubo_default_urls

Locally with Docker (no local daemon):

pytest --ipfs  # test_kubo_default_urls auto-skips, other tests use Docker

Locally with IPFS daemon:

pytest --ipfs  # All tests run

Quick local testing (no IPFS):

pytest  # All IPFS tests skip

CPU and Memory Profiling

We use python's native cProfile for running CPU profiles and snakeviz for visualizing the profile. We use memray for the memory profiling. We will walk through using the profiling tools on the test suite.

Creating the CPU and memory profile requires manual activation of the virtual environment.

source .venv/bin/activate
python -m cProfile -o profile.prof -m pytest
python -m memray run -m pytest

The profile viewers can be directly invoked from uv.

uv run snakeviz .
uv run memray flamegraph <memray output> # e.g. <memray-output> = memray-pytest.12398.bin

Generating documentation

py-hamt uses pdoc. To see a live documentation preview on your local machine, run

uv run pdoc py_hamt

LLMs

If you are an LLM reading this repo, refer to the AGENTS.md file.

Managing dependencies

Use uv add and uv remove, e.g. uv add numpy or uv add pytest --group dev. For more information please see the uv documentation.

Publishing a release

  1. Run uv version <version> and commit the changes to pyproject.toml and uv.lock. Merge the release commit into main and confirm its checks pass.

  2. From a clean checkout of that commit, build the source distribution and wheel, then check their metadata and description rendering.

    uv build --clear --no-sources
    uvx twine check --strict dist/*
    
  3. Use a PyPI API token scoped to py-hamt. In Bash, enter it at the hidden prompt and upload both distributions.

    read -rs -p 'PyPI API token: ' release_token
    UV_PUBLISH_TOKEN="$release_token" uv publish dist/*
    unset release_token
    
  4. Check that both files appear on the PyPI project page. Then create the GitHub release with the matching v<version> tag pointing to the commit you built. The GitHub tag does not upload files to PyPI.

Metadata

Release files for py-hamt 3.7.0

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

Source distribution (sdist)

Source distribution for py-hamt 3.7.0
File Size Uploaded Provenance Actions
py_hamt-3.7.0.tar.gz 334.9 kB Without provenance Details for py_hamt-3.7.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-hamt 3.7.0
File Interpreter ABI Platform Actions
py_hamt-3.7.0-py3-none-any.whl Python 3 none any Details for py_hamt-3.7.0-py3-none-any.whl

Total release size: 419.5 kB

Release files / py_hamt-3.7.0.tar.gz

Download URL py_hamt-3.7.0.tar.gz
Size 334.9 kB
Tags Source
SHA-256 checksum
How to use checksums
68b271cedbeebe1005ecc22f94fb48c21ddb8b88787db6d6bed06d989b48f741
BLAKE2b-256 checksum
How to use checksums
455295e02faff4245eb3861c19bec2839ebe2773fc789015c301c588c7f78687
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / py_hamt-3.7.0-py3-none-any.whl

Download URL py_hamt-3.7.0-py3-none-any.whl
Size 84.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ae73a38350c57b5b1b6f877ca01c7db7f82c882b14aca7eb25efc31a3ce3aa42
BLAKE2b-256 checksum
How to use checksums
1e6edb9ba297ee8ef9ef6874ffb34c89150abf0114f9cb1041219b5b01fcf26d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for py-hamt 3.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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