Python library to represent numbers with units
Project description
units
Python library to represent quantities with units.
Supported Python versions: 3.10+
Python 2 is not supported.
Project layout:
- public facade:
src/units - API exports:
src/api - business logic:
src/core - data models:
src/models - utilities:
src/utils - tests:
tests/unitandtests/integration
Preferred API:
from units import Quantity
from units.si import metre, second, newton
distance = 10 * metre
time = 2 * second
speed = distance / time
force = 5 * newton
print(distance)
print(speed)
print(force)
The preferred construction style is scalar-by-unit multiplication:
from units.si import metre, second
length = 3 * metre
time = 2 * second
speed = length / time
volume = 5 * metre ** 3
Because ** binds more tightly than *, 5 * metre ** 3 is interpreted as
5 * (metre ** 3), which is the intended geometric-unit behavior.
The explicit constructor remains supported and is still the right low-level form when you want to be fully explicit:
from units import Quantity
from units.si import metre
length = Quantity(3, metre)
Legacy API compatibility:
import units as u
print(u.Unit(1, u.metre))
The legacy Unit constructor remains available as a compatibility alias for
Quantity during the migration period, but new code should prefer
from units import Quantity and from units.si import ....
The package is Python 3-only. Python 2 compatibility behavior is not part of the supported interface.
Migration guide
Old style:
import units as u
distance = u.Unit(3, u.metre)
time = u.Unit(2, u.second)
speed = distance / time
New style:
from units.si import metre, second
distance = 3 * metre
time = 2 * second
speed = distance / time
volume = 5 * metre ** 3
Still supported when you want the fully explicit constructor form:
from units import Quantity
from units.si import metre, second
distance = Quantity(3, metre)
time = Quantity(2, second)
speed = distance / time
Public API
Stable top-level imports:
QuantityUnit(compatibility alias forQuantity)UnitsError,InvalidUnitError,InvalidValueError,UnitCompatibilityError,UnitOperandError
Canonical unit imports:
from units.si import metre, second, newton
Legacy compatibility helpers:
long_quantitylong_unit
These names remain available as compatibility aliases for integer conversion in
Python 3, but new code should prefer int_quantity.
Notes on semantics
- Addition and subtraction require identical units.
- Multiplication and division combine units algebraically.
- Integer powers of units and unit-bearing quantities are supported.
- Unitless quantities are supported explicitly.
- The core quantity model allows signed values. Domain-specific constraints such as non-negative lengths should be enforced by higher-level types or validators.
Real-world examples
Electrical engineering: from resistance to power dissipation
from units.si import ampere, ohm, volt, watt
current = 12 * ampere
resistance = 8 * ohm
voltage = current * resistance
power = voltage * current
print(voltage) # 96 V
print(power) # 1152 W
This works because the package canonicalizes unambiguous derived-unit assemblies:
ampere * ohm -> voltvolt * ampere -> watt
Pump sizing: hydraulic power from pressure rise and flow rate
from units.si import metre, second, kilogram, pascal, watt
density = 998 * (kilogram / metre ** 3)
flow_velocity = 2.5 * (metre / second)
pipe_area = 0.0314 * metre ** 2
pressure_rise = 180000 * pascal
volumetric_flow = flow_velocity * pipe_area
hydraulic_power = pressure_rise * volumetric_flow
print(volumetric_flow) # m^3·s^-1
print(hydraulic_power) # W
This is a good example of a multi-step engineering computation that still renders to intuitive derived units at the end of the chain.
Structural mechanics: work from force over distance
from units.si import metre, newton
force = 4200 * newton
displacement = 0.35 * metre
work = force * displacement
print(work) # J
Geometric quantities: powers of units
from units.si import metre
volume = 5 * metre ** 3
area = (12 * metre) ** 2
print(volume) # 5 m^3
print(area) # 144 m^2
The unit form is also valid on its own:
from units.si import metre
area_unit = metre ** 2
volume_unit = metre ** 3
Fluid mechanics: dynamic pressure
from units.si import kilogram, metre, pascal, second
density = 1.225 * (kilogram / metre ** 3)
velocity = 68 * (metre / second)
dynamic_pressure = 0.5 * density * velocity * velocity
print(dynamic_pressure) # Pa
Custom unit systems
Custom unit systems are supported, but they are intentionally separate from SI canonicalization. Use them when you want the same algebra and formatting behaviour without forcing your units into the SI registry.
from units import CustomUnitBase, DimensionSystem
class CommUnit(CustomUnitBase):
dimension_system = DimensionSystem('comm', ('b', 's', 'B'))
bit = CommUnit.define('b')
second = CommUnit.define('s')
data = 32 * bit
duration = 4 * second
rate = data / duration
print(rate) # 8.0 b·s^-1
Custom systems inherit useful behaviour:
- dimensional algebra
- string rendering
- incompatibility checks within a system
They do not automatically simplify into SI-derived names such as V, J, or
Pa, and they cannot be mixed with SI units unless you build an explicit bridge.
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 python_units-0.2.0.tar.gz.
File metadata
- Download URL: python_units-0.2.0.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d57482afaf1caff250cbbe84db3c705852cd9e4e92f0e468f5d8adc7da419f2a
|
|
| MD5 |
813c2da216f46c8b0a4549ca0c239fd2
|
|
| BLAKE2b-256 |
7d9a29dd51dad43b508a4cb607175ec7248afdb8340e550303985da2b77b352a
|
Provenance
The following attestation bundles were made for python_units-0.2.0.tar.gz:
Publisher:
publish.yml on sci2pro/python-units
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_units-0.2.0.tar.gz -
Subject digest:
d57482afaf1caff250cbbe84db3c705852cd9e4e92f0e468f5d8adc7da419f2a - Sigstore transparency entry: 1241216258
- Sigstore integration time:
-
Permalink:
sci2pro/python-units@86d36f615156cd0568239604cca45abbcaeb3d63 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/sci2pro
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@86d36f615156cd0568239604cca45abbcaeb3d63 -
Trigger Event:
release
-
Statement type:
File details
Details for the file python_units-0.2.0-py3-none-any.whl.
File metadata
- Download URL: python_units-0.2.0-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acd48dfa3af26c625fe6b4755a6113733e85589c959fe09009850cae7212681b
|
|
| MD5 |
7cda634d234f586d3aba7ab969d2e2dd
|
|
| BLAKE2b-256 |
aa0e3528ad7c1cb34a5b517b29f4f17dc050503f8f0ad17f2131b456aa788a24
|
Provenance
The following attestation bundles were made for python_units-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on sci2pro/python-units
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_units-0.2.0-py3-none-any.whl -
Subject digest:
acd48dfa3af26c625fe6b4755a6113733e85589c959fe09009850cae7212681b - Sigstore transparency entry: 1241216363
- Sigstore integration time:
-
Permalink:
sci2pro/python-units@86d36f615156cd0568239604cca45abbcaeb3d63 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/sci2pro
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@86d36f615156cd0568239604cca45abbcaeb3d63 -
Trigger Event:
release
-
Statement type: