Physical units as first-class values with dimensional analysis
Project description
unitful
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
DimensionErrorwith a clear message on mismatch - Unit conversion:
.to(unit)converts between compatible units, including offset temperature scales (degC,degF,K) - NumPy integration:
np.array([...]) * mreturns aQuantityArray; ufuncs and reduction functions (np.mean,np.sum, etc.) preserve units - Decorators:
@requiresand@returnsenforce dimensions on function arguments and return values - Custom units and dimensions:
define_unitandnew_dimensionextend the registry at runtime - Serialization:
to_json/from_jsonfor plain-dict round-trips; pickle andcopywork 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bad38e6e6dc00563332790dacfde731a94eb2de7e0d0636edd17357ea79d807
|
|
| MD5 |
c76b8c6fc154d3ff03e3eb5c8206c1ce
|
|
| BLAKE2b-256 |
f26ee8619a2131a8ca30f85771b516c56b391a7ee2ce4bdc736606693d26d242
|
Provenance
The following attestation bundles were made for unitful-1.1.0.tar.gz:
Publisher:
publish.yml on nazarhktwitch/unitful
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
unitful-1.1.0.tar.gz -
Subject digest:
6bad38e6e6dc00563332790dacfde731a94eb2de7e0d0636edd17357ea79d807 - Sigstore transparency entry: 1900901460
- Sigstore integration time:
-
Permalink:
nazarhktwitch/unitful@eb169bc4c6e0f4edc5e99d0e3e3710a3691ed4ba -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/nazarhktwitch
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@eb169bc4c6e0f4edc5e99d0e3e3710a3691ed4ba -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13fa4933e34ecfbc11bd72db305cf964dec61d6fe374118bc451f09e49120fdd
|
|
| MD5 |
e44942639b79eb707efeaab242290fc7
|
|
| BLAKE2b-256 |
66b4080453c37db8723ea81f802496635f5d92621b39d15aac9b9605dfbb3949
|
Provenance
The following attestation bundles were made for unitful-1.1.0-py3-none-any.whl:
Publisher:
publish.yml on nazarhktwitch/unitful
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
unitful-1.1.0-py3-none-any.whl -
Subject digest:
13fa4933e34ecfbc11bd72db305cf964dec61d6fe374118bc451f09e49120fdd - Sigstore transparency entry: 1900901559
- Sigstore integration time:
-
Permalink:
nazarhktwitch/unitful@eb169bc4c6e0f4edc5e99d0e3e3710a3691ed4ba -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/nazarhktwitch
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@eb169bc4c6e0f4edc5e99d0e3e3710a3691ed4ba -
Trigger Event:
push
-
Statement type: