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.2.tar.gz (50.5 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.2-cp314-cp314t-win_amd64.whl (245.3 kB view details)

Uploaded CPython 3.14tWindows x86-64

misu-2.0.2-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (344.2 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ x86-64

misu-2.0.2-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (327.0 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

misu-2.0.2-cp314-cp314t-macosx_11_0_arm64.whl (308.7 kB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

misu-2.0.2-cp314-cp314t-macosx_10_12_x86_64.whl (329.2 kB view details)

Uploaded CPython 3.14tmacOS 10.12+ x86-64

misu-2.0.2-cp39-abi3-win_amd64.whl (249.2 kB view details)

Uploaded CPython 3.9+Windows x86-64

misu-2.0.2-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.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (334.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.9+macOS 11.0+ ARM64

misu-2.0.2-cp39-abi3-macosx_10_12_x86_64.whl (337.1 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: misu-2.0.2.tar.gz
  • Upload date:
  • Size: 50.5 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.2.tar.gz
Algorithm Hash digest
SHA256 8c5b96a88d2f219a72866e753fd3fa48c82f9206227009a879fda63643a554b4
MD5 82a12ae5da413f6d7713fc8ba5368c1f
BLAKE2b-256 79b1e58ef733661026e1526a29c5a85858585d89775348c96cff013ac82139be

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.2.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.2-cp314-cp314t-win_amd64.whl.

File metadata

  • Download URL: misu-2.0.2-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 245.3 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.0.2-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 4751cd40c70cad98cea0ba8adf55394e86e95d9d5e5ee93f6c40bde2b86e6b41
MD5 34ac203b5ce388c729f51c12cf81211d
BLAKE2b-256 87dc2a742a51800f2fac20d6709aac01ba9a7769ae81ad4d1d6aff73a061b61d

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.2-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.0.2-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for misu-2.0.2-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c0d24ce7170ffd896c1fe4e1dcc53ab93dc3e130598b8479db5f3cd4b3e737cf
MD5 abebe6cdae2a783f90bf83ca267fa847
BLAKE2b-256 c58e6b4bae8ba0a8e76b0686efc6b98096b00f74e0d004b5b5f20be84d4ebd5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.2-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.0.2-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for misu-2.0.2-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c8e917ac0e8978bb160f75372f05a1f13604ae33258ba46411f6098c9bd10579
MD5 42178a44fbabffa55dd42ead01761494
BLAKE2b-256 60421da28454318fde9965f7e099b03d01147ed24c492a534a909ab16ffc00c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.2-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.0.2-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for misu-2.0.2-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 65e4197751c73283dec67e407f7d35b3bea3bd73c425f25459cb808082818d68
MD5 9914d0bf50f06729a5343b9a4331acf9
BLAKE2b-256 3dd1234a33a5aaa748981443023c2986937f9845dea161db8175b9569d546717

See more details on using hashes here.

Provenance

The following attestation bundles were made for misu-2.0.2-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.0.2-cp314-cp314t-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for misu-2.0.2-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 402c772fe09742e5e4d724cdd9132b774671723b1774a1ee2e3d11c7d98049c8
MD5 7958ba4ec463422b05f1c0f03c0da2d3
BLAKE2b-256 e2e47655e8cab85b2ec47746634fd44aa6614d6839d858ac00a1de1201f56a3f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: misu-2.0.2-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 249.2 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.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 20010cfbfb02d1786520334a5be104f1bf8ac3333ee2fcb7eb0c96c8b8679fe3
MD5 b4c971fcd7101e6a8d1c761a219084a8
BLAKE2b-256 c34f3df5a9bbdd17c52baaebcb647d62e6acb76e99b953dd84c1728314851e49

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for misu-2.0.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 77aecb965ad81370218acf2088fab0bb09a25d97933d37325aad9571a8d4f605
MD5 f207ce539a12043ed77833ecd5f3a764
BLAKE2b-256 91eaa490670e161ab2b92972ad62c523b5a6d3fb562d0061889f3bd5bc5045c9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for misu-2.0.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 de14d24392f2c9fdb460f8f3bcc8979669747b2bb02eb5a5cbc4d540f97e203b
MD5 e04a8aa8382b0d9e50b4ed14ebf01bce
BLAKE2b-256 b2d1165fe49c2553c101264d0746a030963a0b308f0f710380163e1678f3ee41

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: misu-2.0.2-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.0.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 79f9639d564ed045dd993552b43d82940587c6e4abc6524984ec1504a3bfa03a
MD5 2528f556f8ea17590935e9b3a5c8c184
BLAKE2b-256 2fe4b2e20f1db4c682709bb52bb4c3b164cf0b8d58954040e47caf2b731c5736

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: misu-2.0.2-cp39-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 337.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.0.2-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1516b7c3d71368b98625fb45c5758a6a0a35b251541f8f526f68eb671785aa42
MD5 84dad95cd68e7898403bd842559109da
BLAKE2b-256 08afef83ae09128bf4bb093ab654145ed3b10b38ae54427f6e6e71ccebbb543e

See more details on using hashes here.

Provenance

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