Skip to main content

KV backed dictionary, similar to RedisJSONDict

Project description

CI

kv-dict

KV backed dictionary, similar to RedisJSONDict

Install Package

  • pip: pip install git+https://github.lightsource.ca/daq/kv-dict
  • uv: uv add git+https://github.lightsource.ca/daq/kv-dict
    • add --optional FEATURE to add as an optional dependency
  • pixi: pixi add --git https://github.lightsource.ca/daq/kv-dict
    • add --feature FEATURE to add as an optional dependency

Development

This project uses uv for managing project dependencies. Installation instructions may be found here.

git clone https://github.lightsource.ca/daq/kv-dict.git
cd kv-dict
uv sync --all-extras

Remote Mapping + In-Memory Backend Example

Run a basic RemoteKVMapping flow backed by InMemoryAsyncBackend:

uv run python examples/remote_mapping_in_memory_example.py

Remote Mapping + Redis/Dragonfly Backend Example

Run a basic RemoteKVMapping flow backed by a Redis-compatible server:

uv run python examples/remote_mapping_redis_example.py

[!NOTE] In the devcontainer compose setup, the Redis-compatible service hostname is redis.

Remote Mapping + PostgreSQL Backend Example

Run a basic RemoteKVMapping flow backed by PostgreSQL:

uv run python examples/remote_mapping_postgres_example.py

[!NOTE] In the devcontainer compose setup, PostgreSQL is available on the internal hostname/port postgres:5432.

Remote Mapping + NATS JetStream KV Backend Example

Run a basic RemoteKVMapping flow backed by NATS JetStream KV:

uv run python examples/remote_mapping_nats_example.py

[!IMPORTANT] This example uses create_bucket=False (production-style). Ensure the kv_dict bucket already exists.

[!NOTE] In the devcontainer compose setup, NATS is also available to host-side tools at nats://127.0.0.1:14222.

Watch the changes in the NATS KV with the CLI client:

nats -s localhost:14222 kv watch kv_dict
Create the kv_dict bucket locally (for testing)

Run this once before executing the NATS example:

uv run python - <<'PY'
import asyncio
import nats


async def main() -> None:
  nc = await nats.connect(servers=["nats://nats:4222"])
  try:
    js = nc.jetstream()
    try:
      await js.key_value("kv_dict")
      print("Bucket already exists: kv_dict")
    except Exception:
      await js.create_key_value(bucket="kv_dict")
      print("Created bucket: kv_dict")
  finally:
    await nc.close()


asyncio.run(main())
PY

Differences from Python dict

RemoteKVMapping is intentionally dict-like, but not a byte-for-byte replacement of the built-in dict behavior.

Key behavioral differences

  • Iteration order is sorted by key, not insertion order.
  • Values are persisted through backend round-trips, so operations are not purely in-memory.
  • Nested dict and list mutations are write-through.
  • Mutable non-dict/non-list values are not automatically write-through.

Missing / non-parity APIs

  • fromkeys() is not implemented.

Practical guidance

[!WARNING] RemoteKVMapping is backend-backed, not purely in-memory. Avoid relying on insertion-order behavior and reassignment-free updates for mutable non-list, non-dict nested values.

  • Treat this mapping as a backend-backed structure, not an in-memory object.
  • For nested non-dict updates, reassign the modified value to persist changes.
  • If strict insertion-order semantics are required, do not rely on iteration order from RemoteKVMapping.

Dict parity roadmap

  • Implement copy() semantics for backend-backed snapshots.
  • Implement in-place dict union operator (|=).
  • Implement dict union operator (|).
  • Implement fromkeys() with explicit persistence semantics.
  • Add write-through wrappers for list operations.
  • Add write-through wrappers for additional mutable container types (if required).
  • Decide and document stable ordering strategy (sorted vs insertion-order).
  • Add dedicated parity tests against a reference dict behavior matrix.

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

kv_dict-0.1.6.tar.gz (120.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kv_dict-0.1.6-py3-none-any.whl (27.6 kB view details)

Uploaded Python 3

File details

Details for the file kv_dict-0.1.6.tar.gz.

File metadata

  • Download URL: kv_dict-0.1.6.tar.gz
  • Upload date:
  • Size: 120.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kv_dict-0.1.6.tar.gz
Algorithm Hash digest
SHA256 b3c5506bef0e96b53118f7e7da64a8fdec2f0b57155a9f177f9b09e41c4d29af
MD5 031e5e229327ec73b42f444c05d027aa
BLAKE2b-256 ce57b19a4d141d5844159e7b06eca52de5d0a71d842fe20a783fe62567b03717

See more details on using hashes here.

File details

Details for the file kv_dict-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: kv_dict-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 27.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kv_dict-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4181d8d015a9d76b48eeb6ab08dce1bdf1ea3254b451c4c11dbd423cab8da6b6
MD5 c9375563c5916c4002165feff1269525
BLAKE2b-256 2fb326fee19f594bd75e1f77e06f81a03f21cd06956836d6ca062a8a89f3b1b0

See more details on using hashes here.

Supported by

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