Skip to main content

larzunits

Unit conversion with dimensional checking. Pure Python, zero dependencies.

Convert between units and refuse nonsense: metres to feet is fine, metres to kilograms raises. Handles the affine temperature scales (C/F/K) correctly, and a Quantity type carries a value-with-unit so you can add and compare safely.

from larzunits import convert, Quantity

convert(5, "km", "mi")                 # 3.106...
convert(100, "C", "F")                 # 212.0
convert(1, "KiB", "byte")              # 1024.0

Quantity(5, "m") + Quantity(30, "cm")  # 5.3 m
Quantity(1, "km") > Quantity(500, "m") # True
Quantity(60, "mi") + Quantity(1, "kg") # raises IncompatibleUnits

Why

  • Catches mistakes. Converting or adding across dimensions raises IncompatibleUnits instead of silently returning garbage.
  • Temperature done right. C/F/K are affine (offsets, not just factors) — 0 °C = 32 °F = 273.15 K — and larzunits handles that, where a naive factor table gets it wrong.
  • Batteries of units. Length, mass, time, temperature, data (decimal kb and binary KiB), and volume — imperial and metric.
  • A real Quantity type. to(), +/- (dimension-checked), scalar *//, quantity/quantity ratio, equality and ordering by underlying value.
  • Zero dependencies. No pint, no quantities.

Install

pip install larzunits

Usage

from larzunits import convert, Quantity, compatible, dimension

convert(12, "in", "ft")           # 1.0
compatible("m", "ft")             # True
dimension("mph") if False else dimension("m")   # 'length'

q = Quantity(90, "min")
q.to("h").value                   # 1.5
(Quantity(1, "km") / Quantity(250, "m"))   # 4.0  (dimensionless)

Units: length (m km cm mm um nm mi yd ft in nmi), mass (kg g mg t lb oz st), time (s ms min h day week), temperature (K C F), data (bit byte kb mb gb tb KiB MiB GiB TiB), volume (l ml cl gal pt cup).

Tests

python -m unittest discover -s tests -v   # 21 tests incl. temperature + errors

The Larz stack

One of 30+ pure-Python, zero-dependency libraries at github.com/larz-scripter.

License

MIT © larz-scripter

Metadata

Release files for larzunits 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for larzunits 0.1.0
File Size Uploaded Provenance
larzunits-0.1.0.tar.gz 5.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for larzunits 0.1.0
File Interpreter ABI Platform
larzunits-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / larzunits-0.1.0.tar.gz

Download URL larzunits-0.1.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e25cd02e5ad7ea0dfa80eb21184f65a52956c9e9f8d68ea9aeb167b398ac5d41
BLAKE2b-256 checksum
How to use checksums
ec26374a485855f269c894b0436cbb05d06746d36a7faedd2c48db38ff2281ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / larzunits-0.1.0-py3-none-any.whl

Download URL larzunits-0.1.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb7e1b3778a2208a47bdd91c1e17ed203b768e4ddf4c446c2f19b94b816441ba
BLAKE2b-256 checksum
How to use checksums
a0a63270a510881dc0477e6df56814dcbcbabd1e3d13c298a80048ae496d286a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for larzunits 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page