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.2.0.tar.gz (12.2 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.2.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kanne-2.2.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","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.2.0.tar.gz
Algorithm Hash digest
SHA256 a106279a63bcdd7bc12100f3d14e5f996aa6f049bf4dbc07475e235339cde89f
MD5 f00d5c9d544c7b0e467db92daca540c3
BLAKE2b-256 d241baf543408204c424b0af32c82b3299c4604311bf3392bbf970f19135c2a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kanne-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10534968efa9cbf150592147316dc0471510a132cc04e4de7bfe6af7bb741e3b
MD5 c817dbd91dec00bf97a02b0ccd4e236e
BLAKE2b-256 eca2e373d714ff074abea295040793978787ad1b81d97274f50cf2da99824b0a

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