Package: unitclass
unitclassunitclassis a physical unit class suitable for calculations in the sciences.
This library provides aUnitclass that encapsulates a numerical value with a
physical unit. It is intended for both interactive and library use.unitclasssupports allSI
unitsand prefixes, as well as
every reasonably common English/Imperial unit and other special units (e.g.
bytes and ppm).Usage ExamplesUnit()takes strings or numbers and strings. Any number appended to a unit is
assumed to be an exponent. e.g.m2ism²andin3isin³. You can create
compound units with*and/operators, e.g.N*morft/min. There should
only be one division operator in a unit, but you can have any number of
multiplied units on the left and right sides of the division operator. e.gN*s2/m*kgis interpreted as $\frac{N \cdot s^2}{m \cdot kg}$.Basic Usage>>>fromunitclassimportUnit>>>Unit('1 in')# number and unit in a string1in>>>Unit(1.0,'in')# number and unit as separate arguments1in>>>Unit(1,'in','mm')# convert on-the-fly from one unit to another25.4mm>>>a=Unit(1,'in')>>>b=Unit(1,'ft')>>>a*12+b24inExponents>>>fromunitclassimportUnit>>>Unit('1 m3')1m³>>>Unit('1 in4')1in⁴>>>Unit('1 m3').to('in3')61023.7in³>>>Unit('10 in2')/Unit('1 in')10inCompound Units>>>Unit('1 lbf*ft*s2')1lb·ft·s²>>>Unit(100,'ft/min')100ft/min>>>Unit('1 N*s2/(m*kg)')1N·s²/(m·kg)>>>Unit(100,'ft')/Unit(1,'min')100ft/minConversion>>>fromunitclassimportUnit>>>Unit(1,'in','mm')# convert on-the-fly from one unit to another25.4mm>>>b=Unit(1,'ft')>>>b.to('in')# convert method12in>>>b.to('mm')304.8mm>>>Unit('1 N*m').to('in*lb')8.85075in·lb>>>Unit(100,'ft/min').to('mph')1.13636mph>>>Unit(100,'ft/min').to('kph')1.8288kphListing/Searching Built-in UnitsTo see what units are available (output is abbreviated below):>>>importunitclassasuc>>>uc.list_units()s->unitoftimealiases:['second','seconds','sec','secs']...You can also limit the search to a certain quantity:>>>importunitclassasuc>>>uc.list_units(qty='data')B->unitofdataaliases:['byte','bytes']KB->unitofdataaliases:['kilobyte','kilobytes']MB->unitofdataaliases:['megabyte','megabytes']GB->unitofdataaliases:['gigabyte','gigabytes']TB->unitofdataaliases:['terabyte','terabytes']PB->unitofdataaliases:['petabyte','petabytes']EB->unitofdataaliases:['exabyte','exabytes']Tip: For a list of available quanities, use the functionlist_quantities().
Example usage is below in the Custom Unit section.And you can search for a certain string in a unit or unit alias:>>>importunitclassasuc>>>uc.list_units(qty='data',search='ga')MB->unitofdataaliases:['megabyte','megabytes']GB->unitofdataaliases:['gigabyte','gigabytes']>>>uc.list_units(search='mile')mi->unitoflengthaliases:['mile','miles','statutemile','statutemiles','smi']nmi->unitoflengthaliases:['nauticalmile','nauticalmiles']gmi->unitoflengthaliases:['geographicalmile','geographicalmiles']mph->unitofspeedaliases:['mileperhour']Simplifying and Expanding UnitsTheexpand()method expands the unit to its fundamental units whilesimplify()combines units to a single compound unit if one exists for the
given combination of units. For all options, typehelp(Unit.expand)orhelp(Unit.simplify)at an interactive prompt.>>>a=Unit('1 W')/Unit('1 A')>>>a1W/A>>>a.expand()1m²·kg/(A·s³)>>>a.simplify()1VAdd Custom UnitIn the example below, a custom unit is being added. The unit measures the
quantity "length", the unit is called "blake", two aliases for that unit are
"blakes" and "bunits", and 1 blake equals 6 ft.The fields are as follows:<quantity>, <name>, <aliases>, <factor>, <factor unit>Once the custom unit is added, it can be used the same as any other built-in unit.>>>importunitclassasuc>>>uc.add_unit("length","blake","blakes bunits",6,'ft')>>>c=Unit(12,'in','blakes')>>>c0.166667blake>>>Unit(12*12,'in','blakes')2blakeYou can also bulk load custom units from a CSV file. The CSV would take the same
form as theadd_unit()function above. Here is an example CSV with two custom
units:length, myin, myinch my_inch, 1/8.0, in
angle, myang,, 1/1e-12*sin(2*pi), radAnd then it is loaded with theimport_units()method:>>>importunitclassasuc>>>uc.import_units('customunits.csv')When adding custom units, it is helpful to know whatquantitiesare available.
(E.g. length, time, force, etc.) These are the quantities that are being
measured, or the categories of measurement, not the units themselves. To list
them all, use thelist_quantities()method (the output has been abbreviated
below):>>>importunitclassasuc>>>uc.list_quantities()absorbed_doseaccelerationamountangleangularspeedarea...speedtimetorqueunitlessvoltagevolumeConverting without using the Unit classYou can skip creating a Unit class if you prefer to just do a quick conversion.>>>importunitclassasuc>>>uc.convert(1,'in','mm')25.4>>>uc.convert(55,'mph','kph')88.51391999999998>>>uc.convert(40,'lb/ft3','kg/m3')640.7385327602261CaveatsForce/MassBecause people expect to convert from pounds to kilograms (i.e. force to mass), this library
will automatically handle conversion to/from forces and masses when explicit conversion is requested. This is accomplished by dividing or multiplying by the acceleration of gravity as needed, which makes conversion between force and mass intuitive for the layman
and convenient for the rest.TemperatureBecause of the nature of the temperature scales, a simple multiplier does not
work, so temperature is handled independently of the other units. This leads to
a the limitations that you cannot have custom or compound units with
temperature. This is a rare use case, so fixing this limitation is a low
priority.