Skip to main content

Physical units as first-class values with dimensional analysis

Project description

unitful

PyPI version Python CI License

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.1.0.tar.gz (25.5 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.1.0-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for unitful-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6bad38e6e6dc00563332790dacfde731a94eb2de7e0d0636edd17357ea79d807
MD5 c76b8c6fc154d3ff03e3eb5c8206c1ce
BLAKE2b-256 f26ee8619a2131a8ca30f85771b516c56b391a7ee2ce4bdc736606693d26d242

See more details on using hashes here.

Provenance

The following attestation bundles were made for unitful-1.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: unitful-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.2 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13fa4933e34ecfbc11bd72db305cf964dec61d6fe374118bc451f09e49120fdd
MD5 e44942639b79eb707efeaab242290fc7
BLAKE2b-256 66b4080453c37db8723ea81f802496635f5d92621b39d15aac9b9605dfbb3949

See more details on using hashes here.

Provenance

The following attestation bundles were made for unitful-1.1.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