Subsections

Requesting Conversions

DimPy contains an infix parser which can also handle requests involving quantities. This can be accessed using the interactive session:
>>> python quantity_parser.py
or within the DimPy namespace using the parse function:
>>> dimpy.parse("3 meters")
3.0 m

Computation

Given an expression, DimPy will try to calculate its value and return an answer in SI units. A line is printed showing how the request was interpreted and the result:
---> 3 meters/(2 hours)*4 seconds
3*meter/(2*hour)*4*second = 0.00166666666667 m
The parser accepts two forms of multiplication and each will give a different interpretation. The * symbol behaves as the standard Python multiplication, so any expression appearing after it will begin on the numerator. Alternatively, a space may be inserted which will be interpreted as multiplication with much higher precedence. In general, the natural way to write the sentence dictates which should be used:
---> 1.0/ten million
1.0/(ten*million) = 1e-07
---> 1.0/ten*million
1.0/ten*million = 100000.0
---> 1/newton meter
1/(newton*meter) = 1.0 m^-2 kg^-1 s^2
---> 1/newton*meter
1/newton*meter = 1.0 kg^-1 s^2

Prefixes and Suffixes

A word is any string of non-whitespace characters, seperated by whitespace. The parser will first of all consider a scalar multiple at the start of a word. It will then look for a SI prefix, followed by some quantity and then for a `s', to see if the word is plural. A number may then follow to represent an exponent. This exponent will act on the quantity and prefix, but not the scalar multiple. See QuantityParser.SI_PREFIXES and QuantityParser.SI_PREFIXES_SHORT for a full list of long and short prefixes and their values. A quantity may be any type defined in DimPy (including a defined variable, 5.4) except for a QuantMatrix or Dimension. Therefore, the most general word is of the form:
---> 1e3millimeters2
1*10^3*(0.001*meter)^2 = 0.001 m^2
However, all of these components are optional.

History

When in interactive mode, the quantity_parser module stores a history of recent queries. To view this history, enter
---> print_history()
at the prompt and index:value pairs are displayed. To recall a previous request enter # followed by the corresponding index anywhere in a request:
---> 3 meters in miles
3*meter = 0.00186411357671 * mile
---> print_history()
0: 3 meters in miles
---> #0*2
(3*meter)*2 = 6.0 m
To delete an entry from the history enter:
---> delete("entry index")
Be aware that after a history entry is deleted, the indices of all the entries after it are decreased by one. As recalling histories is dynamic (i.e. only the index to recall is stored) any references that exist in the history will point to different requests after the deletion.
---> 3 meters in miles
3*meter = 0.00186411357671 * mile
---> 2 seconds in hours
2*second = 0.000555555555556 * hour
---> #0*2
(3*meter)*2 = 6.0 m
---> delete(0)
---> #1             (#1 is "#0*2" which now evaluates to (2*second)*2)
((2*second)*2) = 4.0 s
The up and down keys may also be used to recall previous inputs, as in the standard Python session.


Defining variables

Another way of storing values in an interative quantity_parser session is to define variables. To do this simply write an expression of the form
---> new_variable = 3 meters
new_variable = 3*meter
The string on the left hand side must contain only letters and underscores, the right hand side must be any valid expression. To recall the value, use the variable name as for a regular variable.
---> new_variable = 3 meters
new_variable = 3*meter
---> new_variable*4
new_variable*4 = 12.0 m
One advantage of declaring variables over using the history is that variables are not dynamic. The value on the right hand side is evaluated when defining the variable and then stored, so the value of a variable will not change during the session (unless it is redefined).

The online converter

The online converter behaves as the offline parser, except that the history is displayed within the webpage and entries are deleted using the check boxes.

David Bate 2008-09-04