Package: python-units
Python library to represent numbers with unitsExample:importunitsasuprintu.Unit(1,u.metre)Upcoming featuresShort and long units (currently only short units)Multipliers e.g. kilo (k), mega (M), kibi (KiB), ergs etc.Arbitrary units e.g. bits (b), cars etc.FeaturesBasic unitsimportunitsasufromrandomimportrandint# ampereprintu.Unit(randint(1,100),u.ampere)# candelaprintu.Unit(randint(1,100),u.candela)# kelvinprintu.Unit(randint(1,100),u.kelvin)# kilogramprintu.Unit(randint(1,100),u.kilogram)# metreprintu.Unit(randint(1,100),u.metre)# moleprintu.Unit(randint(1,100),u.mole)# secondprintu.Unit(randint(1,100),u.second)Operations on unitsimportunitsasufromrandomimportrandomx=u.Unit(random(),u.second)y=u.Unit(random(),u.ampere)# additionprintx+yprinty+x# subtractionprintx-yprinty-x# multiplicationprintx*yprinty*x# division/true divisionprintx/yprinty/x# floor divisionprintx//yprinty//x# modulusprintx%y# powerprintx**yprinty**xOperations between scalars and unitsimportunitsasufromrandomimportrandomx=u.Unit(random(),u.second)a=random()*10# additionprinta+yprinty+a# subtractionprinta-yprinty-a# multiplicationprinta*yprinty*a# division/true divisionprinta/yprinty/a# floor divisionprinta//yprinty//a# modulusprinta%y# powerprinta**yprinty**aDerived unitsimportunitsasufromrandomimportrandom# hertzprintu.Unit(random()*10,u.hertz)# newtonprintu.Unit(random()*10,u.newton)# pascalprintu.Unit(random()*10,u.pascal)# jouleprintu.Unit(random()*10,u.joule)# wattprintu.Unit(random()*10,u.watt)# coulombprintu.Unit(random()*10,u.coulomb)# voltprintu.Unit(random()*10,u.volt)# faradprintu.Unit(random()*10,u.farad)# ohmprintu.Unit(random()*10,u.ohm)# siememsprintu.Unit(random()*10,u.siemens)# weberprintu.Unit(random()*10,u.weber)# teslaprintu.Unit(random()*10,u.tesla)# henryprintu.Unit(random()*10,u.henry)# degree celciusprintu.Unit(random()*10,u.degree_celcius)# lumenprintu.Unit(random()*10,u.lumen)# luxprintu.Unit(random()*10,u.lux)# becquerelprintu.Unit(random()*10,u.becquerel)# grayprintu.Unit(random()*10,u.gray)# sievertprintu.Unit(random()*10,u.sievert)# katalprintu.Unit(random()*10,u.katal)Unpacking derived unitsimportunitsasufromrandomimportrandom# hertzprintu.Unit(random()*10,u.hertz).full_units# newtonprintu.Unit(random()*10,u.newton).full_units# pascalprintu.Unit(random()*10,u.pascal).full_units# jouleprintu.Unit(random()*10,u.joule).full_units# wattprintu.Unit(random()*10,u.watt).full_units# coulombprintu.Unit(random()*10,u.coulomb).full_units# voltprintu.Unit(random()*10,u.volt).full_units# faradprintu.Unit(random()*10,u.farad).full_units# ohmprintu.Unit(random()*10,u.ohm).full_units# siememsprintu.Unit(random()*10,u.siemens).full_units# weberprintu.Unit(random()*10,u.weber).full_units# teslaprintu.Unit(random()*10,u.tesla).full_units# henryprintu.Unit(random()*10,u.henry).full_units# degree celciusprintu.Unit(random()*10,u.degree_celcius).full_units# lumenprintu.Unit(random()*10,u.lumen).full_units# luxprintu.Unit(random()*10,u.lux).full_units# becquerelprintu.Unit(random()*10,u.becquerel).full_units# grayprintu.Unit(random()*10,u.gray).full_units# sievertprintu.Unit(random()*10,u.sievert).full_units# katalprintu.Unit(random()*10,u.katal).full_unitsArbitrary derived unitsimportunitsasuspeed=u.DerivedUnit.define('speed',metre/second)v=Unit(10,speed)Arbitrary custom unitsfrom__future__importdivisionfromunitsimportBaseUnitclassCommUnit(BaseUnit):"""Template class for communication units"""def__init__(self,*args,**kwargs):super(CommUnit,self).__init__(*args,**kwargs)# redefine the base unitsself.unit_dict={'b':0,'s':0,'B':0,}@classmethoddefdefine(cls,key,value=1):"""Constructor"""obj=cls()assertkeyinobj.unit_dict.keys()assertisinstance(value,int)obj.unit_dict[key]=valuereturnobjwhich will be used as followsbit=CommUnit.define('b')# define a bit as referring to the 'b' unitsecond=CommUnit.define('s')# a second is 's'data=Unit(32,bit)T=Unit(4,second)# data rateprintdata/T# 8.0 b·s^-1 - bits per second