Skip to main content

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

Project description

https://travis-ci.org/cjrh/misu.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/badge/install-pip%20install%20misu-ff69b4.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 will be around only 5X slower when used with misu. 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.

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
    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.

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.0.1.tar.gz (50.3 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.0.1-cp39-abi3-win_amd64.whl (248.8 kB view details)

Uploaded CPython 3.9+Windows x86-64

misu-2.0.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (348.8 kB view details)

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

misu-2.0.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (334.2 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

misu-2.0.1-cp39-abi3-macosx_11_0_arm64.whl (315.8 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

misu-2.0.1-cp39-abi3-macosx_10_12_x86_64.whl (337.2 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for misu-2.0.1.tar.gz
Algorithm Hash digest
SHA256 229fe979dab8233a22475c9dc89b58a9072e445f97bb18054a1dcee044f4eb86
MD5 163e87faef74c65b759f6855adbc10e2
BLAKE2b-256 93d078a69f91be636aae56bc774c00226bae9856f052ef56637646dca5fffb14

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.1.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.0.1-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: misu-2.0.1-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 248.8 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.0.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 85dedf896c8e916159fddbefeaac09bcec2910c5cd854bc12fa8871710dc2165
MD5 ebdbdb2412c3cea5c5211369520b2b0d
BLAKE2b-256 f181edff74316aeddbb5c03eef0d866697395eed259aa9168a50fc051b2baeda

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.1-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.0.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for misu-2.0.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bcf3e653ee572978bb4b1206a428daab00c2f70304ae63b8bdca852541e12bca
MD5 c235c66f8eb729563b9523096a3f7b50
BLAKE2b-256 116ce717a37c8f66980783985db00d167cec39a410f1d35dae74eabb505f0cfe

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.1-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.0.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for misu-2.0.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 22b4b5722e27c2a9caef62918d531a0f7699f7dc3132960e445102bb8ed0ec8c
MD5 9be98dc8d4388d4858bf1fb3845755f8
BLAKE2b-256 d8ffc6d848a8c1ca27a4784d4e50ac003839ae7248ecb080770324e9a69dd231

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.1-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.0.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: misu-2.0.1-cp39-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 315.8 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.0.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1fa892211191bffbe9c090de9d87f0c1294f992114fe270ed0ce3899ea7f1f02
MD5 3ae787eee16407b1703d9f91832cd8ec
BLAKE2b-256 b612ca23f432125ae9754281df004225ec82d4d3bf743e7956f1053ae5f5fd16

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.1-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.0.1-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: misu-2.0.1-cp39-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 337.2 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.0.1-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 39a7c418097d147b4da12e17935b82a515bcf6867bfb25d2d9d9a6d5516988e7
MD5 033f76d2e2c27fbc436940ee1ee6add2
BLAKE2b-256 9c6da47421876103b8eca0233f465b7e0cf3821fdd5528632db49015ea695f85

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.1-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