Merge branch 'doc_changes' of git://github.com/rsking84/pint into rsking84-doc_changes

This commit is contained in:
Hernan Grecco
2014-10-30 16:56:24 -03:00
2 changed files with 44 additions and 1 deletions

View File

@@ -108,6 +108,15 @@ calculate, for example, the wavelength in water of a laser which on air is 530 n
>>> f.to('nm', 'sp', n=1.33)
<Quantity(398.496240602, 'nanometer')>
Contexts can also accept Pint Quantity objects as parameters. For example, the 'chemistry'
context accepts the molecular weight of a substance (as a Quantity with dimensions of
[mass]/[substance]) to allow conversion between moles and mass.
.. doctest::
>>> substance = 95 * ureg('g')
>>> moles = substance.to('moles', 'chemistry', mw = 5 * ureg('g/mol'))
<Quantity(19.0, 'mole')>
Defining contexts in a file
@@ -129,7 +138,8 @@ The `@context` directive indicates the beginning of the transformations which ar
All parameters are named and default values are mandatory. Multiple parameters
are separated by commas (like in a python function definition). Finally, you provide the name
of the context (e.g. spectroscopy) and, optionally, a short version of the name (e.g. sp)
separated by an equal sign.
separated by an equal sign. See the definition of the 'chemistry' context in default_en.txt
for an example of a multiple-parameter context.
Conversions rules are specified by providing source and destination dimensions separated
using a colon (`:`) from the equation. A special variable named `value` will be replaced
@@ -140,6 +150,8 @@ A single forward arrow (`->`) indicates that the equations is used to transform
from the first dimension to the second one. A double arrow (`<->`) is used to
indicate that the transformation operates both ways.
Context definitions are stored and imported exactly like custom units definition file
(and can be included in the same file as unit definitions). See "Defining units" for details.
Defining contexts programmatically
----------------------------------

View File

@@ -318,3 +318,34 @@ firkin = barrel / 4
[temperature] -> [energy]: boltzmann_constant * value
[energy] -> [temperature]: value / boltzmann_constant
@end
@context(mw=0,volume=0,solvent_mass=0) chemistry = chem
# mw is the molecular weight of the species
# volume is the volume of the solution
# solvent_mass is the mass of solvent in the solution
# moles -> mass require the molecular weight
[substance] -> [mass]: value * mw
[mass] -> [substance]: value / mw
# moles/volume -> mass/volume and moles/mass -> mass / mass
# require the molecular weight
[substance] / [volume] -> [mass] / [volume]: value * mw
[mass] / [volume] -> [substance] / [volume]: value / mw
[substance] / [mass] -> [mass] / [mass]: value * mw
[mass] / [mass] -> [substance] / [mass]: value / mw
# moles/volume -> moles requires the solution volume
[substance] / [volume] -> [substance]: value * volume
[substance] -> [substance] / [volume]: value / volume
# moles/mass -> moles requires the solvent (usually water) mass
[substance] / [mass] -> [substance]: value * solvent_mass
[substance] -> [substance] / [mass]: value / solvent_mass
# moles/mass -> moles/volume require the solvent mass and the volume
[substance] / [mass] -> [substance]/[volume]: value * solvent_mass / volume
[substance] / [volume] -> [substance] / [mass]: value / solvent_mass *
volume
@end