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)

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.

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-2.1.tar.gz (32.4 kB view details)

Uploaded Source

Built Distribution

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

u-2.1-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for u-2.1.tar.gz
Algorithm Hash digest
SHA256 af82467fbeb97b1500799bd7c26bb6f46d891670938d9c46185e525a9d029993
MD5 40213340ce932675ec4f4e113fc094ff
BLAKE2b-256 8374acec941caa339b59d9513ad709938fea6a4dc0339783d7b2224949a90499

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for u-2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b26a76331d87cb1baced742fe05aab1bd822e267bd4dbbd1abcddde3dec73b51
MD5 d667b4b0b50ba98859dd40a11003a1c1
BLAKE2b-256 311a85ec690292cf7210a13c90af6af9b2b957aac813b670ea8950da74d5153d

See more details on using hashes here.

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