Skip to main content

u

Statically typed units.

Quickstart

Type safe assignments:

duration: u.Duration = u.seconds(5)  # Ok
distance: u.Distance = u.amperes(5)  # Type checking error

Type safe math:

print(u.seconds(120) + u.minutes(3))  # Ok
print(u.seconds(120) + u.amperes(3))  # Type checking error

Type safe derived units:

SPEED = u.DIV[u.DISTANCE, u.DURATION]
Speed = u.Quantity[SPEED]

speed: Speed = u.km(5) / u.hours(1)  # Ok
speed: Speed = (u.km / u.hours)(5)  # Also ok

ELECTRIC_CHARGE = u.MUL[u.DURATION, u.ELECTRIC_CURRENT]
ElectricCharge = u.Quantity[ELECTRIC_CHARGE]

charge: ElectricCharge = u.sec(3) * u.amperes(2)  # Ok
charge: ElectricCharge = u.amperes(2) * u.sec(3)  # Also ok

Reusable prefixes:

print(u.megabytes(5) == u.mega(u.bytes)(5))  # True

Define your own quantities and units:

class TASTINESS(u.QUANTITY):
    pass

Tastiness = u.Quantity[TASTINESS]

mmm = u.Unit(Tastiness, symbol='mmm', multiplier=1)
yum = u.Unit(Tastiness, 'yum', 10)

taste: Tastiness = yum(42)

Convert to string:

thousand_meters = u.meters(1000)
print(thousand_meters)             # Automatically selects a suitable unit: "1 km"
print(f'{thousand_meters:m}')      # Uses the specified unit: "1000 m"
print(f'{thousand_meters:cm:m}')   # Selects a unit from the given range: "1000 m"
print(f'{thousand_meters:.1f m}')  # Formats the number: "1000.0 m"

Caveats

Since type checkers don't understand math, calculations involving different types of quantities sometimes cause type checkers to complain even though they're correct. For example:

area: u.Area = (u.m2 * u.kelvins / u.kelvins)(1)  # Type checking error

Documentation

There is no online documentation, but everything has docstrings and type annotations. Your IDE should be able to show you all the relevant information.

Metadata

Release files for u 5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for u 5.0
File Size Uploaded
u-5.0.tar.gz 46.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for u 5.0
File Interpreter ABI Platform
u-5.0-py3-none-any.whl Python 3 none any Details

Total release size: 82.0 kB

Release files / u-5.0.tar.gz

Download URL u-5.0.tar.gz
Size 46.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6ca702a13c83cd83ba39666e11ba893a633d26c403b288e68f434b63d5102310
BLAKE2b-256 checksum
How to use checksums
340dd0e6f79fd76769c0226ceddbdada06412ebbef7e65f804b37a67db0b8c6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.5

Release files / u-5.0-py3-none-any.whl

Download URL u-5.0-py3-none-any.whl
Size 36.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab6ea453fb3c0add36c8833d5a9df40a2caf690c3651fd3c99108f4053187fd1
BLAKE2b-256 checksum
How to use checksums
36db48e38dadd96598b1ddb3c5d019c79bca0a1c2a7992879fc2b19a8828b20d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.5

Release history Release notifications | RSS feed

This release

5.0 This release

2 release files

4.0

2 release files

3.1

2 release files

3.0

2 release files

2.1

2 release files

2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page