Skip to content

Aran-Fey/u

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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: Speed = u.km(5) / u.hours(1)  # Ok
speed: Speed = (u.km / u.hours)(5)  # Also ok

ElectricCharge = u.Mul[u.Duration, u.ElectricCurrent]
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

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

taste: Tastiness = yum(42)

About

Statically typed units for python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages