>>> python quantity_parser.pyor within the DimPy namespace using the parse function:
>>> dimpy.parse("3 meters") 3.0 m
---> 3 meters/(2 hours)*4 seconds 3*meter/(2*hour)*4*second = 0.00166666666667 mThe 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
---> 1e3millimeters2 1*10^3*(0.001*meter)^2 = 0.001 m^2However, all of these components are optional.
---> 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 mTo 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 sThe up and down keys may also be used to recall previous inputs, as in the standard Python session.
---> new_variable = 3 meters new_variable = 3*meterThe 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 mOne 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).
David Bate 2008-09-04