misu is short for “misura”, which means measurement (in Italian).
Most of the time you will probably work with misu interactively, and it will be most convenient to import the entire namespace:
from misu import * mass = 100*kg print(mass >> lb)
The symbol kg got imported from the misu package. We redefine the shift operator to perform inline conversions. The code above produces:
There are many units already defined, and it is easy to add more. Here we convert the same quantity into ounces:
print(mass >> oz)
What you see above would be useless on its own. What you really need is to be able to perform consistent calculations with quantities expressed in different, but compatible units:
mass = 10*kg + 20*lb print(mass)
For addition and subtraction, misu will ensure that only consistent units can be used. Multiplication and division will produce new units:
distance = 100*metres time = 9.2*seconds speed = distance / time print(speed)
As before, it is trivially easy to express that quantity in different units of compatible dimensions:
speed >> km/hr print(speed >> km/hr)
misu is a package of handling physical quantities with dimensions. This means performing calculations with all the units being tracked correctly. It is possible to add kilograms per hour to ounces per minute, obtain the correct answer, and have that answer be reported in, say, pounds per week.
misu grew out of a personal need. I have used this code personally in a (chemical) engineering context for well over a year now (at time of writing, Feb 2015). Every feature has been added in response to a personal need.
@dimensions(x='Length', y='Mass') def f(x, y): return x/y f(2*m, 3*kg) # Works f(200*feet, 3*tons) # Works f(2*joules, 3*kelvin) # raises AssertionError f(2*m, 3) # raises AssertionError
mass = 100 * kg mass_lb = mass >> lb duty = 50 * MW duty_BTU_hr = duty >> BTU / hr
There are several units systems for Python, but the primary motivating use-case is that misu is written as a Cython module and is by far the fastest* for managing units available in Python.
*Except for “NumericalUnits“, which is a special case
**I haven’t actually checked that this statement is true for all of them yet.
For speed-critical code, the application of unit operations can still be too slow. In these situations it is typical to first cast quantities into numerical values (doubles, say), perform the speed-critical calculations (perhaps call into a C-library), and then re-cast the result back into a quantity and return that from a function.
@dimensions(x='Length', y='Mass'): def f(x, y): x = x >> metre y = y >> ounces <code that assumes meters and ounces, returns value in BTU> return answer * BTU
This way you can still easily wrap performance-critical calculations with robust unit-handling.
The inspiration for misu was Frink by Alan Eliasen. It is wonderful, but I need to work with units in the IPython Notebook, and with all my other Python code.
There are a bunch of other similar projects. I have not used any of them enough yet to provide a fair comparison:
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|misu-1.0.3-cp27-cp27m-macosx_10_5_x86_64.whl (142.5 kB) Copy SHA256 Checksum SHA256||cp27||Wheel||Mar 13, 2016|
|misu-1.0.3-cp27-cp27m-win_amd64.whl (106.6 kB) Copy SHA256 Checksum SHA256||cp27||Wheel||Mar 13, 2016|
|misu-1.0.3-cp34-cp34m-macosx_10_5_x86_64.whl (141.3 kB) Copy SHA256 Checksum SHA256||cp34||Wheel||Mar 13, 2016|
|misu-1.0.3-cp34-none-win_amd64.whl (102.1 kB) Copy SHA256 Checksum SHA256||cp34||Wheel||Mar 13, 2016|
|misu-1.0.3-cp35-cp35m-macosx_10_5_x86_64.whl (139.6 kB) Copy SHA256 Checksum SHA256||cp35||Wheel||Mar 13, 2016|
|misu-1.0.3-cp35-cp35m-win_amd64.whl (106.7 kB) Copy SHA256 Checksum SHA256||cp35||Wheel||Mar 13, 2016|
|misu-1.0.3.tar.gz (158.1 kB) Copy SHA256 Checksum SHA256||–||Source||Mar 13, 2016|