Skip to main content

Statically typed units

Project description

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)

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

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

u-1.0.4.tar.gz (29.7 kB view details)

Uploaded Source

Built Distribution

u-1.0.4-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file u-1.0.4.tar.gz.

File metadata

  • Download URL: u-1.0.4.tar.gz
  • Upload date:
  • Size: 29.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for u-1.0.4.tar.gz
Algorithm Hash digest
SHA256 5dac1e4b7fd19f89876252027bd88a02ecfbffa3f273b1641c36f994c4377c5d
MD5 9c51824dda456700bd1582cc0babb0a4
BLAKE2b-256 12b9399c77a6a0d33c7c6df65bcde9a3157bdeb4b66f550d2e24364ee809b6e5

See more details on using hashes here.

File details

Details for the file u-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: u-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for u-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fe3fff1c5a405e0a2666424d5026d5219f9b167b05f0257702962f64e472bce2
MD5 98fd8d98923a9fd942d644d60223c315
BLAKE2b-256 51208ebe629b642d75de47a6087629cce4a32550e4da20e36dac9d5f27c823e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page