Skip to main content

a context-aware si-unit registry manager built on pint and pydantic

Project description

kanne

codecov PyPI version Maintainer PyPI pyversions PyPI status PyPI download day

kanne

Kanne is a small utility package to help manage unit-aware quantities and a simple registry/context pattern for parsing and validating units using Pint. It provides convenience helpers, validators, and a minimal context manager (Kanne) to make working with Pint registries explicit and testable.

Key ideas:

  • Keep unit parsing and validation ergonomic.
  • Provide a lightweight context aware registry (Kanne) for libraries.
  • Export commonly-used units and small helpers for convenience.

📦 Installation

uv add kanne

⚡ Quick Start

Kanne provides a context manager to manage Pint registries easily.

Fields are typed by physical dimension (Duration, Length, …), not by a fixed unit. A field accepts any unit-bearing value of the right dimension, preserves the unit you gave it, and serializes to an abbreviated pint string that the matching server library parses back. A bare number (no unit) is rejected, and so is a value of the wrong dimension.

from kanne.kanne import Kanne
from kanne import Duration, Length
from kanne.registry import KanneRegistry
from pydantic import BaseModel


class Event(BaseModel):
    duration: Duration   # any time quantity
    extent: Length       # any spatial length


with Kanne(registry=KanneRegistry()):
    event = Event(duration="1500 ms", extent="2.5 um")

    event.model_dump()       # {'duration': '1500 ms', 'extent': '2.5 µm'} — unit preserved
    event.duration.to("s")   # <Quantity(1.5, 'second')>

    Event(duration="3 m", extent="1 m")   # raises — a length is not a duration
    Event(duration=1500, extent="1 m")    # raises — bare numbers are ambiguous

    # arithmetic stays dimensionful
    event.duration + event.extent          # raises — incompatible dimensions

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

kanne-2.0.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

kanne-2.0.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file kanne-2.0.0.tar.gz.

File metadata

  • Download URL: kanne-2.0.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for kanne-2.0.0.tar.gz
Algorithm Hash digest
SHA256 7281a95b9a9c58a0528108c3bc089c0e685f564e87af8a02fc5a9cdae4fed360
MD5 25c347a1d990c36070a86a4f3dab4370
BLAKE2b-256 5d462479a3cbd73e796c5b08b1dbd3e74ba618f1fcb00b123f637e83f111e141

See more details on using hashes here.

File details

Details for the file kanne-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: kanne-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for kanne-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d6cc0432cf6f911dc1aa405f9dab29335ad42947ac16f0508ede40432e309e5
MD5 36d68ab85d3972b5a622efcce47b0996
BLAKE2b-256 7614d6f9cb6d04abdf8c0360268fa8b125e3e69d0dc9c23b09abf4498cc2aa63

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