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.1.1.tar.gz (12.1 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.1.1-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kanne-2.1.1.tar.gz
  • Upload date:
  • Size: 12.1 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.1.1.tar.gz
Algorithm Hash digest
SHA256 dde9e5cb338110f2b67bb5c617f4a6a5c28ec2284a92b48d72e48f1b170b58ac
MD5 69eff6366ffd76c3403fdd0d00e7281c
BLAKE2b-256 189e4a51bea195e6542252e413e3e0d179c7fbb6c8ad44cd7d39e84e29ccb048

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kanne-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.3 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 66f490dce14a6275a3294666e4730cd94c8d5eb10d0c743f5738b74f9f9aa054
MD5 937146ae5443cfcedd25079bb7ede811
BLAKE2b-256 9334448feae4d91d6a744d60b7a4b599f62651a92de8e2c7bbe3def24bbfea0d

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