Module magnitude
source code
A physical quantity is a number with a unit, like 10 km/h. Units can be any
of the SI units, plus a bunch of non-SI, bits, dollars, and any combination
of them. They can include the standard SI prefixes. Magnitude can operate
with physical quantities, parse their units, and print them. You don't have
to worry about unit consistency or conversions; everything is handled
transparently. By default output is done in basic SI units, but you can
specify any output unit, as long as it can be reduced to the basic units of
the physical quantity.
The basic units understood by the magnitude module are:
indicator meaning
--------- -------
$ dollar ('dollar' is also acceptable)
A amperes
bit binary digit
cd candela
K degrees Kelvin
kg kilograms
m meters
mol amount of substance
s seconds
From these basic units you can derive many other units. The magnitude
package predefines these derived units:
Bq becquerel
C coulomb
c speed of light (m/s)
day
degC degree Celsius
dpi dots per inch
F farad
ft feet ("'" is also acceptable)
g gram
gravity acceleration due to gravity (m/s**2)
Gy gray
H henry
h hour
Hz Hertz
inch ('"' is also acceptable)
ips inches per second
J joule
kat byte
l liter
lightyear light year
lm lumen
lpi lines per inch
lux
min minute
N newton
ohm
Pa pascal
S siemens
Sv sievert
T tesla
V volt
W watt
Wb weber
year
Two magnitudes have no units, 'rad' (radian - unit of plane angle) and 'sr'
(steradian - unit of solid angle).
Any of the above units can be augmented with the following set of scale
prefixes:
letter scale name
------ ----- ----
y 1e-24 yocto
z 1e-21 zepto
a 1e-18 atto
f 1e-15 femto
p 1e-12 pico
n 1e-9 nano
u 1e-6 micro
m 1e-3 mili
c 1e-2 centi
d 1e-1 deci
k 1e3 kilo
Ki 2^10 Kibi
M 1e6 mega
Mi 2^20 Mebi
G 1e9 giga
Gi 2^30 Gibi
T 1e12 tera
Ti 2^40 Tebi
P 1e15 peta
Pi 2^50 Pebi
E 1e18 exa
Ei 2^60 Exbi
Z 1e21 zetta
Y 1e24 yotta
You can define new magnitudes by instantiating the Magnitude class. Suppose
you want to define pounds as a magnitude and associate with it the unit
'lb'. A pound is 0.45359237 kilograms, so we have
>>> lb = Magnitude(0.45359237, kg=1)
To make it recognized automatically you also have to present it to the system:
>>> new_mag('lb', lb)
You can then use it as you would any other predefined physical quantity:
>>> me = mg(180, 'lb')
>>> print me.ounit('kg').toval()
81.6466266
The following online references provide more detail about physical units and
the SI system.
http://physics.nist.gov/cuu/Units/units.html
http://en.wikipedia.org/wiki/SI
http://www.gnu.org/software/units/units.html for units.dat
http://www.cip.physik.uni-muenchen.de/~tf/misc/etools.lisp
- This code was very much inspired by
- http://www.cs.utexas.edu/users/novak/units.html
- and its associated paper,
- http://www.cs.utexas.edu/users/novak/units95.html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mg(v,
unit='',
ounit='')
Builds a Magnitude from a number and a units string |
source code
|
|
|
|
ensmg(m,
unit='')
Ensures that something is a Magnitude. |
source code
|
|
|
|
|
|
|
|
|
|
new_mag(indicator,
mag)
Define a new magnitude understood by the package. |
source code
|
|
Change the default ouptut format. Include a fmt if and where
you need to specify the output precission. Defaults to %.*f,
where the * stands for the precission. Returns the default
format.
|
Change the output precission. Default is 4. Returns it.
|
Set the units output. Default is True.
|