Skip to main content

Fast quantities (units of measurement) for Python — Rust-powered.

Project description

https://github.com/cjrh/misu/actions/workflows/ci.yml/badge.svg?branch=master https://coveralls.io/repos/github/cjrh/misu/badge.svg?branch=master https://img.shields.io/pypi/pyversions/misu.svg https://img.shields.io/github/tag/cjrh/misu.svg https://img.shields.io/pypi/v/misu.svg

misu

misu is short for “misura”, which means measurement (in Italian). misu is a package for doing calculations with in consistent units of measurement.

Install

Precompiled wheels are published to PyPI for Linux (x86_64, aarch64), macOS (x86_64, aarch64), and Windows (x64), covering Python 3.9 and later. There is nothing to compile.

uv add misu        # in a uv project
uv pip install misu
pip install misu

Demo

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:

220.46226218487757

There are many units already defined, and it is easy to add more. Here we convert the same quantity into ounces:

print(mass >> oz)

output:

3571.4285714285716

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)

output:

19.07 kg

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)

output:

10.87 m/s

As before, it is trivially easy to express that quantity in different units of compatible dimensions:

print(speed >> km/hr)

output:

39.130434782608695

Introduction

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.

Features

  • Speed optimized. misu is very fast! Heavy math code in Python is only around 5X slower when expressed with misu quantities instead of plain floats — see Performance below for measured numbers. This is much faster than other quantities packages for Python.

  • Implemented as a Rust extension module via PyO3, so the hot paths run as native code.

  • When an operation involving incompatible units is attempted, an EIncompatibleUnits exception is raised, with a clear explanation message about which units were inconsistent.

  • Decorators for functions to enforce dimensions

@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
  • An operator for easily stripping the units component to obtain a plain numerical value

mass = 100 * kg
mass_lb = mass >> lb

duty = 50 * MW
duty_BTU_hr = duty >> BTU / hr
  • An enormous amount of redundancy in the naming of various units. This means that m, metre, metres, METRE, METRES will all work. The reason for this is that from my own experience, when working interactively (e.g. in the IPython Notebook) it can be very distracting to incorrectly guess the name for a particular unit, and have to look it up. ft, foot and feet all work, m3 means m**3 and so on.

  • You can specify a reporting unit for a dimension, meaning that you could have all lengths be reported in “feet” by default for example.

  • You can specify a reporting format for a particular unit.

Performance

Two looping numerical workloads, each timed in plain Python floats and then with misu quantities (so every operation in the inner loop pays the unit-tracking cost). The benchmark lives at scripts/benchmark.py and can be re-run any time:

python scripts/benchmark.py

Representative numbers on Python 3.14, best of five runs:

Workload

float (ms)

misu (ms)

slowdown

fall_with_drag — 200k Euler steps, 1-D free-fall with quadratic drag

~19

~103

~5.2x

orbit_step — 100k 2-D Kepler steps, sqrt-heavy

~18

~91

~5.1x

Geometric mean

~5.1x

The original 5x heuristic was measured back when misu was a Cython extension; the rewrite to a Rust/PyO3 extension lands in the same ballpark, presumably because the dominant cost is the Python-call boundary around each __mul__ / __add__ rather than the unit-arithmetic itself.

There are other projects, why misu?

There are several units systems for Python, but the primary motivating use-case is that misu is written as a Rust extension 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.

General usage

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   # Converts to metres, leaving a primitive float
    y = y >> ounces  # Converts to metres, leaving a primitive float
    <code that assumes meters and ounces, returns value in BTU>
    # Convert the primitive float to BTU on the way out
    return answer * BTU

This way you can still easily wrap performance-critical calculations with robust unit-handling.

Inspiration

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:

Releasing

Publishing to PyPI is automated via GitHub Actions and PyPI trusted publishing (OIDC). No API tokens or passwords are stored anywhere — PyPI trusts the cjrh/misu repo’s release.yml workflow running in the pypi environment, and rejects everything else.

Cutting a release

The version lives in Cargo.toml. pyproject.toml declares dynamic = ["version"], so maturin reads it from the Rust crate at build time — there is only one place to edit.

Use cargo-release to bump, commit, tag, and push in one step. From a clean master:

cargo release patch --execute   # 2.0.0 → 2.0.1
cargo release minor --execute   # 2.0.0 → 2.1.0
cargo release major --execute   # 2.0.0 → 3.0.0
cargo release 2.0.5 --execute   # explicit version

Drop --execute for a dry run.

What happens automatically

cargo-release performs the following steps locally:

  1. Bumps version in Cargo.toml and updates Cargo.lock.

  2. Commits the change with the message Release <version>.

  3. Creates an annotated tag v<version>.

  4. Pushes the branch and the tag to origin.

The tag push triggers .github/workflows/release.yml, which:

  1. Builds wheels for Linux (x86_64, aarch64), macOS (x86_64, aarch64), and Windows (x64), plus an sdist. Because PyO3 is configured with abi3-py39, one wheel per (OS, arch) covers all supported Python versions.

  2. Downloads all artifacts into the release job, which runs in the pypi GitHub environment.

  3. Uploads to PyPI via pypa/gh-action-pypi-publish. Authentication happens via OIDC against the trusted-publisher configuration on PyPI; nothing else is needed.

If the build jobs succeed but the release job fails (for example, the PyPI environment was not configured), nothing is published, and the release can be retried by re-running just the failed job.

Troubleshooting

  • cargo-release refuses with “uncommitted changes” — commit or stash first; cargo-release insists on a clean tree.

  • cargo-release refuses with “not on allowed branch” — release only from master (the default allow-branch setting).

  • PyPI rejects the upload with “version already exists” — PyPI filenames are immutable; bump again.

  • Tag pushed but workflow did not run — check the tag matches the tags: '*' trigger in release.yml and that the pypi GitHub environment exists with no protection rules blocking the run.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

misu-2.1.0.tar.gz (54.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

misu-2.1.0-cp314-cp314t-win_amd64.whl (240.0 kB view details)

Uploaded CPython 3.14tWindows x86-64

misu-2.1.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (339.8 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ x86-64

misu-2.1.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (324.5 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

misu-2.1.0-cp314-cp314t-macosx_11_0_arm64.whl (306.1 kB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

misu-2.1.0-cp314-cp314t-macosx_10_12_x86_64.whl (327.4 kB view details)

Uploaded CPython 3.14tmacOS 10.12+ x86-64

misu-2.1.0-cp39-abi3-win_amd64.whl (247.9 kB view details)

Uploaded CPython 3.9+Windows x86-64

misu-2.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (347.2 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

misu-2.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (332.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

misu-2.1.0-cp39-abi3-macosx_11_0_arm64.whl (315.9 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

misu-2.1.0-cp39-abi3-macosx_10_12_x86_64.whl (333.1 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file misu-2.1.0.tar.gz.

File metadata

  • Download URL: misu-2.1.0.tar.gz
  • Upload date:
  • Size: 54.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for misu-2.1.0.tar.gz
Algorithm Hash digest
SHA256 4e9d64d0d2d9804f1873f1c89397e01ecbc31029b41eb11c400a8e37c0d931af
MD5 3d61eb3e3d1800b203662c9e8a792ccb
BLAKE2b-256 76d3e50b2565e5cebacb49a5d3c72ce3eb158f684758ef201c222bf6c4d77e1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0.tar.gz:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp314-cp314t-win_amd64.whl.

File metadata

  • Download URL: misu-2.1.0-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 240.0 kB
  • Tags: CPython 3.14t, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for misu-2.1.0-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 1300be7e501498282e1e60b8df17a4b08e774c085bfdc15d27cd4ebfa595e0ba
MD5 901f596982bd36af14bc91702d96287e
BLAKE2b-256 8aa1b1ae057454d4547c94eaef0e7271227d6396ee565bb10fee0acb20646f22

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp314-cp314t-win_amd64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for misu-2.1.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fd684df194ab328297f81455e91ff783017c477e784e51f6a77f04fb838dfceb
MD5 d3273f44bfb1efb656ec36f3cdc4370d
BLAKE2b-256 9c3c674163e1ce2299b8490eea78de450e957bfe9b7c707dc2c230061eb8d227

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for misu-2.1.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d39aea20a433df56c67b88de0b22258a0086d867aa9e1c8893651b18dec71675
MD5 55ad4cbe0c7d3872a47f7a30e4983343
BLAKE2b-256 9985331bd5dad1d2ced9420dd1a0cf43f2aff92fd1512ecd1f0622d0d43f7e30

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for misu-2.1.0-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 643f532a0cd21269826c66755ca93402b86c7ad6f2d83390a864e51f759aca31
MD5 ef5ef2148cb797a84e51ecca5fe2e64c
BLAKE2b-256 105cbe205a8b9b32d4c2d9806ca106cdbdb3e86490b540595f425bebef2837e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp314-cp314t-macosx_11_0_arm64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp314-cp314t-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for misu-2.1.0-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6b6fabe566a1a41c2526be8bb3766cd0f770ef29444f8545c4db95808783bf71
MD5 5b9f60d924468adef2897e5fe077e118
BLAKE2b-256 11b6d6462fda1a7170e575a2373797a0502291dea5211c70612a464b6b9830f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp314-cp314t-macosx_10_12_x86_64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: misu-2.1.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 247.9 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for misu-2.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0cda3ffeccf50c133acc72bd392dad4cc7ec2323ae3c42571b2ef889a7d65ab3
MD5 68b108d7b44ed69ebe1eaeb9501db711
BLAKE2b-256 7ef44779cadf360e53363cc2b672b006135f2516552a3e8ce24d0b1df092668d

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp39-abi3-win_amd64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for misu-2.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ecacdb5015b1cd71cd497ce8d88a32918fe83891115a5d17bc272eccc433ef57
MD5 a75c4a9575a1bfec21589373fc34e692
BLAKE2b-256 4d985c1d6ea772188b5b9160c9a58264ad353abbb10b279fefa035a9d9718e70

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for misu-2.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1870593ddb8d3923201b87a489a524e66ff2ebcfc5020493959701509b2fb3bb
MD5 8de5afe838f7aa914b5ab30767153294
BLAKE2b-256 081261a26232378fc9fa1183d4be869947e867340d0c58c71ff2941c760ac5aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: misu-2.1.0-cp39-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 315.9 kB
  • Tags: CPython 3.9+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for misu-2.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6237db4b353a1f6afff8491d670f176e8c2ca9d4f5260e7b3bb4e0da12b765cb
MD5 017a44f2b9fbd433688fbd381ef1e9bf
BLAKE2b-256 f6e85889c88333af8947ff905ace5b4a6f52cf3dbee7c323ba50784b7edea30e

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file misu-2.1.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: misu-2.1.0-cp39-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 333.1 kB
  • Tags: CPython 3.9+, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for misu-2.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c39d2af8b6b6d43a9c46bc015c760ec4e7dbe86a8dafa39d198d010287e33ec4
MD5 b89c11f53a60ad2e34a557bffe177f65
BLAKE2b-256 b3e6713c5064b6f6cc3c2352f7885ae38e14bbaf5c9ffce83b6e96aaa82e7fe3

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.1.0-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on cjrh/misu

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page