Package: unit-system-py
unit-system PythonAs of version 0.6.0 all the source code is generated by python scripts.This repository only contains the generated source files.If you want to report a bug or change something check thegenerator repository.This is an implementation of the SI unit system for Python development.
All units, combinations, literals and constants are generated from templates.
If you feel something is missing open an issue or a pull request on thegenerator repository.Usage examplesfromunit_system_pyimportliterals# create some unitsl1=literals.km(10)t1=literals.s(5)# combine the two units to the correct onev1=l1/t1# print the result, with correct unit names# This should print# 10000 meter in 5 second is 2000 meter / secondprint(l1,"in",t1,"is",v1)As you can see using the unit system is pretty easy.
The literals class contains all implemented SI unit names.
You can use them to create units with the correct multiplier and offset.While it is generally better to create units using the literals, it is also possible to include the units directly.
Just note that you have to manually set the multiplier, since the unit is not created using the literals.fromunit_system_pyimporttime,length# create some unitsl1=length(10,1000)t1=time(5)# combine the two units to the correct onev1=l1/t1# print the result, with correct unit names# This should print# 10000 meter in 5 second is 2000 meter / secondprint(l1,"in",t1,"is",v1)This allows to specify multipliers that are not created by the literals.
Another use of this, is to specify type hints for the units.
This is highly encouraged, when defining functions that take units as arguments.fromunit_system_pyimportlength,speed,literalsfrommathimportsin,radians# create an example function which calculates a throw distance from a speed and angledefthrow_distance(throw_speed:speed,angle_degrees:float)->length:# verify that the correct unit is passedifnotisinstance(throw_speed,speed):raiseTypeError("throw_speed must be of type speed")# convert the angle to radiansangle=radians(angle_degrees)# get the speed in m/s and the local gravity accelerationspeed_mps=float(throw_speed)g=float(literals.G(1))# calculate the distancereturnspeed_mps**2*sin(2*angle)/g# get the distance for a throw with 10 m/s and 45 degreesprint(throw_distance(literals.mps(10),45))Because not all combinations of units are implemented, you can also cast units to a float.
Calling float on a unit will return the value of the unit in the base unit.
Alternatively you can also use theconvert_tomethod to convert a unit to another multiplier and offset.
If you want to use thevalueproperty directly, make sure to check the multiplier and offset first.
If you don't do this, you might get wrong results since the value ofliterals.km(1)is the same as the value ofliterals.m(1).
They have to be converted to the same multiplier and offset before you can use them directly.
All functions of the unit_system either do this, or they also combine the multipliers and offsets correctly.Units that are currently supportedtime -> time with seconds as base unitlength -> length with meter as base unitmass -> mass with kg as base unittemperature -> temperature with K as base unitamount -> amount of substance with mole as base unitelectric_current -> electric current with Ampere as base unitluminous_intensity -> luminous intensity with candela as base unitarea -> area withm^2as base unitspeed -> speed withm / sas base unitacceleration -> acceleration withm / s^2as base unitmomentum -> momentum withkg * m / sas base unitforce -> force withkg * m / s^2(Newton) as base unitenergy -> energy withkg * m^2 / s^2(Joules) as base unitpower -> power withkg * m^2 / s^3(Watts) as base unitUpgrade InstructionsSince 0.8.0 is the first release, there are no upgrade instructions yet.