python pint : Create a "pace" metric from velocity metrics

49 Views Asked by At

Using the pint library in python - It has support for unit conversions, which work great.

For example:

units.Quantity(2, "mph").to("kph")

I would like to understand how I could define a unit pacem - which would be "minutes per mile", or pacek which would be "minutes per km"

This feels like it should be do-able in the units definitions, without having to write any python code at all, in the same way as I could write

units.define("rps = revolution / second")
1

There are 1 best solutions below

1
Sam Mason On BEST ANSWER

Those units scale with the inverse of each other, i.e. speed is inversely proportional to pace. Hence I seem to need to transform first. Something like:

import pint
units = pint.UnitRegistry()
units.define("pace = minutes / kilometers")
speed = units.Quantity('10 kph')
print((1/speed).to('pace'))

outputs 6.0 pace.