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

Download files

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

Source Distribution

larzunits-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

larzunits-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file larzunits-0.1.0.tar.gz.

File metadata

  • Download URL: larzunits-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzunits-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e25cd02e5ad7ea0dfa80eb21184f65a52956c9e9f8d68ea9aeb167b398ac5d41
MD5 21ec2d860607da47a9acc1519f240959
BLAKE2b-256 ec26374a485855f269c894b0436cbb05d06746d36a7faedd2c48db38ff2281ee

See more details on using hashes here.

File details

Details for the file larzunits-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: larzunits-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzunits-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb7e1b3778a2208a47bdd91c1e17ed203b768e4ddf4c446c2f19b94b816441ba
MD5 704a7fe6c466f742846e3884466a680a
BLAKE2b-256 a0a63270a510881dc0477e6df56814dcbcbabd1e3d13c298a80048ae496d286a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

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