Easy fuzzy unit ratio and conversion helpers.
Project description
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()
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 Distribution
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 easyunits-1.0.0.tar.gz.
File metadata
- Download URL: easyunits-1.0.0.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37ce1df55116417cebc333487c7f374540704e1e8069094da8259d4ee7bb9eca
|
|
| MD5 |
b828b328d568aa4677901d0197a5d512
|
|
| BLAKE2b-256 |
5c57437af5a5a9099dec6d595ef30bd0c9a116a91322c39a3aceb1315f701f85
|
File details
Details for the file easyunits-1.0.0-py3-none-any.whl.
File metadata
- Download URL: easyunits-1.0.0-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9893f8dbf3bcd3f36a46fd231b9f40cc22dc3921178eba5bb42449ce0dc78d0
|
|
| MD5 |
2086b2a3c5f75205c9d30e0c0eb4d550
|
|
| BLAKE2b-256 |
9629cc244f0a9845694b1e3104f0ceb3929ed21318578f6e64cf8ad0c5b6cb75
|