Skip to main content

Physical units as first-class values with dimensional analysis

Project description

unitful

PyPI version Python CI Coverage License Downloads

Physical units as first-class Python values

Attach a unit to any number, arithmetic propagates units automatically Dimension mismatches raise DimensionError at runtime instead of producing silently wrong results

Getting Started

pip install unitful
from unitful import m, s, kg, km, h

distance = 100 * m
time     = 9.58 * s
speed    = distance / time        # Quantity(10.44, 'm/s')

speed.to(km / h)                  # Quantity(37.58, 'km/h')
speed.to(kg)                      # DimensionError: [Length/Time] != [Mass]

Features

  • Dimension checking: all arithmetic validates physical dimensions and raises DimensionError with a clear message on mismatch
  • Unit conversion: .to(unit) converts between compatible units, including offset temperature scales (degC, degF, K)
  • NumPy integration: np.array([...]) * m returns a QuantityArray; ufuncs and reduction functions (np.mean, np.sum, etc.) preserve units
  • Decorators: @requires and @returns enforce dimensions on function arguments and return values
  • Custom units and dimensions: define_unit and new_dimension extend the registry at runtime
  • Serialization: to_json / from_json for plain-dict round-trips; pickle and copy work without extra setup
  • Formatting: f"{q:.2f~P}" (Unicode), f"{q:.2f~L}" (LaTeX), f"{q:.2f~H}" (HTML)

Requirements

Python 3.10 or later. NumPy is optional; install it with:

pip install "unitful[numpy]"

License

MIT

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

unitful-1.0.0.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

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

unitful-1.0.0-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file unitful-1.0.0.tar.gz.

File metadata

  • Download URL: unitful-1.0.0.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for unitful-1.0.0.tar.gz
Algorithm Hash digest
SHA256 07be7a2bb024674b4424f27b5f13b4d009668c1c94d3ad9d0bc4f1e1168d4ed6
MD5 6a195a9adcb7d141afdf37f05f42d6e9
BLAKE2b-256 bda12116ce65360c42c40fd1686ec05d384ff3e9f3a2d8caf0f12f82aa127e02

See more details on using hashes here.

Provenance

The following attestation bundles were made for unitful-1.0.0.tar.gz:

Publisher: publish.yml on nazarhktwitch/unitful

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file unitful-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: unitful-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for unitful-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 163e30f38b6cfe193fee1c6681ce922f640ff14f0120fc5e560cbd8eb5996e82
MD5 db70ea24daf73b9c92e3793c096612c0
BLAKE2b-256 7c007de92ee44742fee6d69540aaabd0c7279d5bcec9c5f51bcab30fe1ad86e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for unitful-1.0.0-py3-none-any.whl:

Publisher: publish.yml on nazarhktwitch/unitful

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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