Package: unitpy
UnitPy (Unit Python)UnitPy is a Python package for defining, converting, and working with units.The goal of the package is to be simple, straightforward, and performant.This package directly implementsNIST (National Institute of Standards and Technology)official unit definitions.InstallationPip installable package:pip install unitpypypi: unitpyRequirements / DependenciesPython 3.8 and upBasic UsageDefining UnitfromunitpyimportU,Q,Unit,Quantity# U = Unitu=Unit("kilometer")u=U("kilometer")u=U("km")u=U.kilometeru=U.kmu=U("kilometer/hour")u=U("km/h")u=U.kilometer/U.houru=U.km/U.h# propertiesprint(u.dimensionality)# [length] / [time]print(u.dimensionless)# Falseprint(u.base_unit)# meter / secondDefining Quantity** Quantity = Value + Unit **fromunitpyimportU,Q,Unit,Quantity# Q = Quantityq=1*U("kilometer/hour")q=1*(U.kilometer/U.hour)q=Quantity("1 km/h")q=Q("1 kilometer per hour")q=Q("1*kilometer/hour")# propertiesprint(q.unit)# kilometer / hourprint(q.dimensionality)# [length] / [time]print(q.dimensionless)# Falseprint(q.base_unit)# meter / secondUnit ConversionfromunitpyimportU,Q,Unit,Quantity# Q = Quantityq=1*U("km/h")q2=q.to("mile per hour")print(q2)# 0.6213711922 mile / hourMathematical operationfromunitpyimportU,Qq=1*U("km/h")q2=2.2*U("mile per hour")print(q2+q)# 2.8213711923 mile / hourprint(q2-q)# 1.5786288077 mile / hourprint(q2*q)# 2.2 kilometer mile / hour**2print(q2/q)# 2.2 mile / kilometerprint((q2/q).dimensionless)# TrueOther supported functions:summaxminTemperatureAbbreviations:fahrenheits: degFcelsius: degCkelvin: degK, Krankine: degRfromunitpyimportU,Qq=300*U("K")q2=200*U("K")print(q+q2)# 500.0 kelvinprint(q.to("degC"))# 26.85 Celsiusprint(q.to("degF"))# 830.6344444444 Fahrenheitprint(q.to("degR"))# 166.6666666667 RankineTemperature units are non-multiplicative units. They are expressed with respect to a reference point (offset).degC = 5/9 * (degF - 32)Default behavior isabsolute units.Forrelative unitsuse dedicated functionsadd_rel()orsub_rel().fromunitpyimportU,Qq=10*U("degC")q2=5*U("degC")# absoluteprint(q.to("K"))# 283.15 kelvinprint(q+q2)# 288.15 Celsiusprint((q+q2).to("K"))# 561.3 kelvinprint(q-q2)# -268.15 Celsiusprint((q-q2).to("K"))# 5.0 kelvin# relativeprint(q.add_rel(q2))# 15 Celsiusprint(q.sub_rel(q2))# 5 Celsiusprint(abs(-10*U.degC))# 10 CelsiusTimefromdatetimeimporttimedeltafromunitpyimportUa=1.234*U.minprint(a)# 1.234 minuteb=a.to_timedelta()print(b)# 0:01:14.040000print(type(b))# <class 'datetime.timedelta'>string formattingfromunitpyimportUa=1.23432453*U.minprint(a)# 1.23432453 minuteprint(f"{a:.2f}")# 1.23 minuteb=1_000_000*U.cmprint(b)# 1000000 centimeterprint(format(b,","))# 1,000,000 centimeterc=123*U.inchprint(c)# 123 inchprint(f"{c:5}")#   123 inch  (leading spaces)print(f"{c:05}")# 00123 inch  (leading zeros)configuration with .envTo configure the string outputs with environment file (.env):installpython-dotenv. The code uses this to load
variables from the .env filepip install python-dotenvcopy the .env file from this repo into the top level of your repo and edit to your likingIt should now load the environmental variables when you run the code to format it to your liking without any
additional code needed.Numpy SupportNumpyimportnumpyasnpfromunitpyimportUnit,Quantity# numpy + inta=np.linspace(0,4,5)*Unit.mb=1*Unit.ftc=a+bprint(a+b)# [0.3048 1.3048 2.3048 3.3048 4.3048] meterprint(a-b)# [-0.3048  0.6952  1.6952  2.6952  3.6952] metera+=bprint(a)# [0.3048 1.3048 2.3048 3.3048 4.3048] meter# numpy + numpya=np.linspace(0,4,5)*Unit.mb=np.linspace(0,4,5)*Unit.ftprint(a+b)# [0.     1.3048 2.6096 3.9144 5.2192] meterprint(a-b)# [0.     0.6952 1.3904 2.0856 2.7808] metera+=bprint(a)# [0.     1.3048 2.6096 3.9144 5.2192] meter# numpy * inta=np.linspace(0,4,5)*Unit.mb=1.2*Unit.sprint(a*b)# [0.  1.2 2.4 3.6 4.8] meter secondprint(a/b)# [0.  0.83333333 1.66666667 2.5 3.33333333] meter/seconda*=bprint(a)# [0.  1.2 2.4 3.6 4.8] meter second# numpy * numpya=np.linspace(0,4,5)*Unit.mb=np.linspace(0,4,5)*Unit.sprint(a*b)# [ 0.  1.  4.  9. 16.] meter secondprint(a/b)# [nan  1.  1.  1.  1.] meter/seconda*=bprint(a)# [ 0.  1.  4.  9. 16.] meter second# numpy functionsa=np.linspace(-2,4,5)*Unit.mprint(np.sum(a))# 5 meterprint(np.max(a))# 4 meterprint(np.abs(a))# [2.  0.5 1.  2.5 4. ] meterprint(np.linspace(1*Unit.mm,2*Unit.ft,4))# [  1.         203.86666667 406.73333333 609.6       ] millimeterNotesthis package utilizes the American spellings "meter," "liter," and "ton"supports pickling