Package Scientific :: Package Physics :: Module PhysicalQuantities :: Class PhysicalQuantity
[frames] | no frames]

Class PhysicalQuantity

Physical quantity with units

PhysicalQuantity instances allow addition, subtraction, multiplication, and division with each other as well as multiplication, division, and exponentiation with numbers. Addition and subtraction check that the units of the two operands are compatible and return the result in the units of the first operand. A limited set of mathematical functions (from module Numeric) is applicable as well:

See the documentation of the PhysicalQuantities module for a list of the available units.

Here is an example on usage:

>>> from PhysicalQuantities import PhysicalQuantity as p  # short hand
>>> distance1 = p('10 m')
>>> distance2 = p('10 km')
>>> total = distance1 + distance2
>>> total
PhysicalQuantity(10010.0,'m')
>>> total.convertToUnit('km')
>>> total.getValue()
10.01
>>> total.getUnitName()
'km'
>>> total = total.inBaseUnits()
>>> total
PhysicalQuantity(10010.0,'m')
>>> 
>>> t = p(314159., 's')
>>> # convert to days, hours, minutes, and second:
>>> t2 = t.inUnitsOf('d','h','min','s')
>>> t2_print = ' '.join([str(i) for i in t2])
>>> t2_print
'3.0 d 15.0 h 15.0 min 59.0 s'
>>> 
>>> e = p('2.7 Hartree*Nav')
>>> e.convertToUnit('kcal/mol')
>>> e
PhysicalQuantity(1694.2757596034764,'kcal/mol')
>>> e = e.inBaseUnits()
>>> str(e)
'7088849.77818 kg*m**2/s**2/mol'
>>> 
>>> freeze = p('0 degC')
>>> freeze = freeze.inUnitsOf ('degF')
>>> str(freeze)
'32.0 degF'
>>>
Instance Methods
 
__abs__(self)
 
__add__(self, other)
 
__cmp__(self, other)
 
__div__(self, other)
 
__init__(self, *args)
There are two constructor calling patterns:
 
__mul__(self, other)
 
__neg__(self)
 
__nonzero__(self)
 
__pos__(self)
 
__pow__(self, other)
 
__radd__(self, other)
 
__rdiv__(self, other)
 
__repr__(self)
 
__rmul__(self, other)
 
__rpow__(self, other)
 
__rsub__(self, other)
 
__str__(self)
 
__sub__(self, other)
 
convertToUnit(self, unit)
Change the unit and adjust the value such that the combination is equivalent to the original one.
 
cos(self)
 
getUnitName(self)
Return unit (string) of physical quantity.
 
getValue(self)
Return value (float) of physical quantity (no unit).
PhysicalQuantity
inBaseUnits(self)
Returns: the same quantity converted to base units, i.e.
PhysicalQuantity or tuple of PhysicalQuantity
inUnitsOf(self, *units)
Express the quantity in different units.
bool
isCompatible(self, unit)
Returns: True if the specified unit is compatible with the one of the quantity
 
sin(self)
 
sqrt(self)
 
tan(self)
Method Details

__init__(self, *args)
(Constructor)

 

There are two constructor calling patterns:

  1. PhysicalQuantity(value, unit), where value is any number and unit is a string defining the unit
  2. PhysicalQuantity(value_with_unit), where value_with_unit is a string that contains both the value and the unit, i.e. '1.5 m/s'. This form is provided for more convenient interactive use.
Parameters:
  • args ((number, str) or (str,)) - either (value, unit) or (value_with_unit,)

convertToUnit(self, unit)

 

Change the unit and adjust the value such that the combination is equivalent to the original one. The new unit must be compatible with the previous unit of the object.

Parameters:
  • unit (str) - a unit
Raises:
  • TypeError - if the unit string is not a know unit or a unit incompatible with the current one

inBaseUnits(self)

 
Returns: PhysicalQuantity
the same quantity converted to base units, i.e. SI units in most cases

inUnitsOf(self, *units)

 

Express the quantity in different units. If one unit is specified, a new PhysicalQuantity object is returned that expresses the quantity in that unit. If several units are specified, the return value is a tuple of PhysicalObject instances with with one element per unit such that the sum of all quantities in the tuple equals the the original quantity and all the values except for the last one are integers. This is used to convert to irregular unit systems like hour/minute/second.

Parameters:
  • units (str or sequence of str) - one or several units
Returns: PhysicalQuantity or tuple of PhysicalQuantity
one or more physical quantities
Raises:
  • TypeError - if any of the specified units are not compatible with the original unit

isCompatible(self, unit)

 
Parameters:
  • unit (str) - a unit
Returns: bool
True if the specified unit is compatible with the one of the quantity