Fast Rust-based Python extension for physical unit manipulation
Project description
pintrs
A fast, Rust-powered drop-in replacement for pint -- the Python physical units library.
pintrs reimplements pint's core unit registry and quantity system in Rust via PyO3, giving you the same Python API with significantly better performance.
Installation
pip install pintrs
Quick start
from pintrs import UnitRegistry
ureg = UnitRegistry()
# Create quantities
distance = ureg.Quantity(5.0, "kilometer")
time = ureg.Quantity(2.0, "hour")
# Arithmetic with automatic unit tracking
speed = distance / time
print(speed) # 2.5 kilometer / hour
print(speed.to("m/s")) # 0.6944... meter / second
# Attribute-style access
print(ureg.meter) # 1 meter
print(ureg.speed_of_light)
Performance
pintrs is 10-90x faster than pint on common operations. Benchmarks on Python 3.13 (lower is better):
| Operation | pintrs | pint | Speedup |
|---|---|---|---|
| Quantity creation | 0.35 us | 3.64 us | 10x |
Parse string ("9.81 m/s**2") |
0.74 us | 70.61 us | 96x |
| Conversion (km -> m) | 1.19 us | 8.08 us | 7x |
| Conversion (km/h -> m/s) | 1.68 us | 14.11 us | 8x |
| Addition (compatible units) | 0.94 us | 12.66 us | 13x |
| Multiply by scalar | 0.13 us | 5.59 us | 41x |
| Multiply quantities | 0.17 us | 5.38 us | 31x |
Parse units ("kg * m / s ** 2") |
0.95 us | 23.66 us | 25x |
| String formatting | 0.29 us | 8.58 us | 29x |
Run python examples/benchmark.py to reproduce (install pint for comparison).
Features
- Drop-in replacement for pint's
UnitRegistry,Quantity,Unit, and common operations - NumPy support via
ArrayQuantitywith full ufunc integration - Type-safe with full
.pyistubs for mypy and pyright in strict mode - Measurement support for quantities with uncertainty propagation
- Compatibility stubs for
Context,Group,Systemso existing code doesn't break
NumPy integration
import numpy as np
from pintrs import UnitRegistry
from pintrs.numpy_support import ArrayQuantity
ureg = UnitRegistry()
distances = ArrayQuantity(np.array([1.0, 2.0, 3.0]), "kilometer", ureg)
result = distances.to("meter")
print(result.magnitude) # [1000. 2000. 3000.]
# NumPy ufuncs work transparently
print(np.sqrt(ArrayQuantity(np.array([4.0, 9.0]), "m**2", ureg)))
Measurements (uncertainty)
from pintrs import Measurement, Quantity
m = Measurement(Quantity(100.0, "meter"), 0.5)
print(m) # 100.0 +/- 0.5 meter
print(m.rel) # 0.005
# Error propagation (adds in quadrature)
m2 = Measurement(Quantity(50.0, "meter"), 0.3)
print(m + m2)
Decorators
from pintrs import UnitRegistry, wraps, check
ureg = UnitRegistry()
@wraps(ureg, ret="meter/second", args=("meter", "second"))
def speed(distance, time):
return distance / time
result = speed(ureg.Quantity(100, "km"), ureg.Quantity(2, "hour"))
print(result) # in m/s
@check(ureg, "[length]", "[time]")
def velocity(d, t):
return d / t
Custom units
ureg = UnitRegistry()
ureg.define("smoot = 1.7018 * meter")
print(ureg.Quantity(1, "smoot").to("meter")) # 1.7018 meter
Compatibility with pint
pintrs targets API compatibility with pint's most-used features:
UnitRegistry,Quantity,Unitwith full arithmetic- Unit parsing, conversion, base/root/compact/reduced/preferred units
__getattr__on registry (ureg.meter,ureg.speed_of_light)- Serialization via
__reduce__(pickle) andto_tuple/from_tuple wrapsandcheckdecoratorsMeasurementwith uncertainty propagation- Context/Group/System stubs (API-compatible, no-op)
Not yet implemented: full context-based conversions (spectroscopy, etc.), Babel/locale formatting, logarithmic units, pandas ExtensionArray integration.
Development
# Build (requires Rust toolchain and maturin)
maturin develop --release
# Lint and format
ruff check --fix . && ruff format .
# Type check
mypy python/pintrs/
pyright python/pintrs/
# Test
pytest
License
Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pintrs-0.1.5.tar.gz.
File metadata
- Download URL: pintrs-0.1.5.tar.gz
- Upload date:
- Size: 60.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7fedb2a761a98fded171fa6f6561bcee80474152eea5e771ce2d2ce277b22877
|
|
| MD5 |
645cd7fc536edde4c7f237d9589f86aa
|
|
| BLAKE2b-256 |
086eeca73f437dbf4e668c09a11c712522bbf57c110afc40336e78b2d31a8568
|
File details
Details for the file pintrs-0.1.5-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 369.0 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7907af58746d188b5fe08a36130a07201b80c34229a59a3f4240e50676d605d9
|
|
| MD5 |
2a07f7fc140d2eb51f2a32c5dba19ef0
|
|
| BLAKE2b-256 |
664cec95000264f7cffb2571d348cdad7b66db77b79377c8633a9b6280e6d4ed
|
File details
Details for the file pintrs-0.1.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 519.7 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3884446a694b27a8ecd2ec6ada64443da619d1fbfb0ee61cab8935b5be974a24
|
|
| MD5 |
52a0b31718430d40d6591beb5c489578
|
|
| BLAKE2b-256 |
0bf8b5483502230fa76b28fddb7391a7808e27688e674c453c675f09f947fad8
|
File details
Details for the file pintrs-0.1.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 507.3 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1259044f0c42154a3e86445505aea19502c0dd7f5dba157e4498238fc6bf3a80
|
|
| MD5 |
a42f3c51d7c6c98680ae3a59203c2372
|
|
| BLAKE2b-256 |
ddb6f14409e567f2be6b2786cd182ddb3526f958e69a409d4e0deb6b6f6a858e
|
File details
Details for the file pintrs-0.1.5-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 464.7 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f66ffb9dba7564224e058914626ef6186be88a799d53b4fe46edc96b092ba28
|
|
| MD5 |
7a74a01b173cb42efa5bfabe702dae13
|
|
| BLAKE2b-256 |
aab68f7fe2fd3e157ee3592776c7e5c3afc2b65d3e52aac4c031c9573d5bdbe6
|
File details
Details for the file pintrs-0.1.5-cp313-cp313-macosx_10_12_x86_64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp313-cp313-macosx_10_12_x86_64.whl
- Upload date:
- Size: 488.3 kB
- Tags: CPython 3.13, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa4616f33bb6fdfd0961d2e554138829a5913722871a756c56263fbb3235ae95
|
|
| MD5 |
076dc175db70c5d621151f0f1fbed9f9
|
|
| BLAKE2b-256 |
756cc05684493d2b967f9ff21337bef9292ccde8dfd6849c9c8c59718eac826b
|
File details
Details for the file pintrs-0.1.5-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 369.7 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2eab8062b4231d3e314f27819dd30cac1593cbae19921fa0935cf10c1623080b
|
|
| MD5 |
d3486930625f7b9fb9091e71257477b8
|
|
| BLAKE2b-256 |
68ab6135a1a248dcd1e5bcc7291fc4ceb2ac3d4b0c4ba54ee3ee4cab90bd22a0
|
File details
Details for the file pintrs-0.1.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 520.1 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13ebca7fbdaace44d3f5ea7e8b14f97acae53d401db53b57b83cd3cb2d1ab1de
|
|
| MD5 |
aff63bc5dbadd5c7e7af6905c4993360
|
|
| BLAKE2b-256 |
4d26644463696b3cdbdff9b421d651584aad82a9b6a8354d8255343e10047ccf
|
File details
Details for the file pintrs-0.1.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 508.4 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07c33e332893a32bb0cc0b2eff02e83c93ae95dbc5aab94fd6a9032931755be4
|
|
| MD5 |
b6e8e718812b09dddb3ba75347676a5b
|
|
| BLAKE2b-256 |
2607797ea566f3be00b80b9cbf009a783be04e2e2905aeb029d0632d34bdde90
|
File details
Details for the file pintrs-0.1.5-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 464.4 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a36b6bbccf639fd8081dcd1643a017de50f8746e32f5812e7ae6d2a8fd270f31
|
|
| MD5 |
f61d620a9af86d2a00e466d6fb37c2b0
|
|
| BLAKE2b-256 |
845436adbe193c35c48a091357f2bbf3f11c659e9e7b956fc71fb112aa6cbe67
|
File details
Details for the file pintrs-0.1.5-cp312-cp312-macosx_10_12_x86_64.whl.
File metadata
- Download URL: pintrs-0.1.5-cp312-cp312-macosx_10_12_x86_64.whl
- Upload date:
- Size: 488.7 kB
- Tags: CPython 3.12, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7955a8014814c36be9558ab8e8010e8c21cd1c2cb06ed87c1af94e6c62e4181c
|
|
| MD5 |
edbb85bb2eb4f22a5d4f434f77c07fa8
|
|
| BLAKE2b-256 |
c5ca34fef3537b11bd708783dd12c5810a6ed12d50108256efa6471624211b20
|