easyunits
Created to be used in a project, this package is published to github for ease of management and installation across different modules.
easyunits is a tiny performance-focused Python library for fuzzy unit ratios and conversions.
It provides category objects with inherited ratio and conversion methods:
{Category}.ratio(from_unit, to_unit): returns the multiplicative ratio between compatible units.{Category}.convert(value, from_unit, to_unit): converts a value from one unit to another.
Temperature uses affine conversion, so Temperature.ratio() supports temperature differences and Temperature.convert() supports normal temperature values.
Installation
From PyPI:
pip install easyunits
For local development from this repository:
python -m pip install -e ".[test]"
Run tests:
python -m pytest
Examples
from easyunits import Length, Temperature, Weight
Length.ratio("inch", "cm")
# 2.54
Weight.convert(10, "Pounds", "kg")
# 4.5359237
Temperature.convert(32, "fahrenheit", "celsius")
# 0.0
Length.convert(12, "INCHES", "ft")
# 1.0
Categories
lengthmass/weighttemperatureareavolumetimespeed
Unit names are fuzzy: plural forms, case differences, punctuation, spaces, and common abbreviations are accepted. For example, Inch, inch, INCH, inches, in., and " all resolve to inches.
Category Objects
Each public category, such as Length, is a singleton object backed by a normal class, such as LengthCategory, that inherits from UnitCategory. This keeps the preferred API compact while letting shared behavior use regular instance methods instead of classmethods.
from easyunits import Length, LengthCategory, UnitCategory
isinstance(Length, LengthCategory)
# True
isinstance(Length, UnitCategory)
# True
Length.INCH
# 0.0254
Length.UNITS["inch"]
# 0.0254
Length.UNIT_ALIASES["inch"]
# ("in", "in.", "inches", '"')
Length.resolve_unit("INCHES")
# "inch"
Length.ratio("inch", "cm")
# 2.54
Length.convert(12, "INCHES", "ft")
# 1.0
To add a unit in the library source, add a constant, a UNITS entry, and optional UNIT_ALIASES entries to the matching category class. If you add units dynamically at runtime, update the category object and call that object's rebuild_index() afterward so fuzzy lookup sees the new aliases and clears cached resolutions.
from easyunits import Length
Length.FURLONG = 201.168
Length.UNITS = {**Length.UNITS, "furlong": Length.FURLONG}
Length.UNIT_ALIASES = {**Length.UNIT_ALIASES, "furlong": ("fur", "furlongs")}
Length.rebuild_index()
Length.convert(2, "fur", "m")
# 402.336
To create a new category, subclass UnitCategory, define NAME, BASE_UNIT, UNITS, and UNIT_ALIASES, instantiate it, then call your_category.rebuild_index() before using fuzzy lookup if you changed its maps after construction.
Each built-in category lives in its own module: length.py, mass.py, temperature.py, area.py, volume.py, time.py, and speed.py. Shared behavior lives in base.py, while errors live in errors.py.
rebuild_index() also checks for fuzzy alias collisions. If two units in the same category normalize to the same alias, it raises UnitError instead of silently overwriting one unit with another.
Public API
Use category objects directly:
Length.ratio(),Length.convert()Mass.ratio(),Mass.convert()Weight.ratio(),Weight.convert()Temperature.ratio(),Temperature.convert()Area.ratio(),Area.convert()Volume.ratio(),Volume.convert()Time.ratio(),Time.convert()Speed.ratio(),Speed.convert()
Metadata
Release files for easyunits 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| easyunits-1.0.0.tar.gz | 14.7 kB | Without provenance | Details for easyunits-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| easyunits-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for easyunits-1.0.0-py3-none-any.whl |
Total release size: 32.0 kB
Release files / easyunits-1.0.0.tar.gz
| Download URL | easyunits-1.0.0.tar.gz |
|---|---|
| Size | 14.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
37ce1df55116417cebc333487c7f374540704e1e8069094da8259d4ee7bb9eca
|
|
BLAKE2b-256 checksum How to use checksums |
5c57437af5a5a9099dec6d595ef30bd0c9a116a91322c39a3aceb1315f701f85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / easyunits-1.0.0-py3-none-any.whl
| Download URL | easyunits-1.0.0-py3-none-any.whl |
|---|---|
| Size | 17.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b9893f8dbf3bcd3f36a46fd231b9f40cc22dc3921178eba5bb42449ce0dc78d0
|
|
BLAKE2b-256 checksum How to use checksums |
9629cc244f0a9845694b1e3104f0ceb3929ed21318578f6e64cf8ad0c5b6cb75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Security for easyunits 1.0.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.