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)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| u-5.0.tar.gz | 46.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|
Security for u 5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.