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.

Release files for u 4.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 4.0
File Size Uploaded
u-4.0.tar.gz 36.6 kB Details

Built distribution (wheel)

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

Total release size: 67.7 kB

Release files / u-4.0.tar.gz

Download URL u-4.0.tar.gz
Size 36.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2824f587ac714381fb64c4162110682b243f3ac0e28d489dac81eeba55f1a884
BLAKE2b-256 checksum
How to use checksums
e4618954a73c8a54e787eb8d8403ccbddabf3f2d5593b5fc17f52634a93be6e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.5

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

Download URL u-4.0-py3-none-any.whl
Size 31.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
923a7f51d365a00cf0ec766a7487c932aff83ce2a1adecc4afc0f720551d5abe
BLAKE2b-256 checksum
How to use checksums
4441cffe6d7044eab5e0a4175b9f9d3b0fabd55630cf243b5695f9a432dcd205
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

4.0 This release

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