Skip to main content

Python library to represent numbers with units

Project description

units

badge.fury.io Python 3.10+ Coverage 92%

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/unit and tests/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. It is deprecated and scheduled for removal in 1.0.0, but it remains a true alias until then so existing type checks keep working. 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:

  • Quantity
  • Unit (compatibility alias for Quantity)
  • UnitsError, InvalidUnitError, InvalidValueError, UnitCompatibilityError, UnitOperandError

Canonical unit imports:

  • from units.si import metre, second, newton

Legacy compatibility helpers:

  • Unit
  • long_quantity
  • int_unit
  • float_unit
  • long_unit
  • complex_unit

These names remain available during the migration period and emit DeprecationWarning when called. Unit remains a true alias for Quantity and does not emit a call-time warning, because preserving Unit is Quantity is part of the pre-1.0.0 compatibility contract. New code should prefer Quantity, scalar-by-unit construction, and the *_quantity conversion helpers. The deprecated compatibility paths are scheduled for removal in 1.0.0.

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 -> volt
  • volt * 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_units-0.3.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

python_units-0.3.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file python_units-0.3.0.tar.gz.

File metadata

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

File hashes

Hashes for python_units-0.3.0.tar.gz
Algorithm Hash digest
SHA256 32865eda5c636f9232bdf6598fa5435c1e432682682dde5f6f718e94f8b8a737
MD5 8b2e8903477729a8dc237d4fb6df5a47
BLAKE2b-256 0127e23c287894dcb59b197121a31108959db4d692789a7fe036e9bd39338728

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_units-0.3.0.tar.gz:

Publisher: publish.yml on sci2pro/python-units

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

File details

Details for the file python_units-0.3.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for python_units-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6afe8c77f454c4c7934da0faa143dead0ce0c4d9c1997cd473d31d9f83e3eed0
MD5 c904becf1d7bd746fc2816e94e30bf69
BLAKE2b-256 a18b9b2a0d4e71e48f57d77167e964a2c532fc4961cce4d2696737a808dc2263

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_units-0.3.0-py3-none-any.whl:

Publisher: publish.yml on sci2pro/python-units

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