Unit Converter Library
Project description
What is Unitflex? 📦
Unitflex is a Python library for converting values between various measurement units such as length, mass, temperature, data, and more.
It provides a clean, extensible, and user-friendly interface to perform conversions easily — with customizable output formatting and high precision modes for professional use.
Designed to be both beginner-friendly and robust enough for engineers, Unitflex is suitable for daily tasks, education, and advanced scientific projects.
It even supports a special "engineering" mode for precise calculations using decimal.Decimal.
Supported Converters 🧪
1. Length (unitflex.length)
Units: mm, cm, m, km, in, ft, yard, mil, mile, nm, um, pm, dm, angstrom, nmi
2. Mass (unitflex.mass)
Units: mg, g, kg, lb, ton, quintal, ounce, oz, st, slug, dram, carat, grain, shortton, longton
3. Temperature (unitflex.temper)
Units: celsius, fahrenheit, kelvin, rankine, reaumur
Short names: c, f, k, r, re
Symbols: °c, °f, k, °r, °re
4. Data (unitflex.data)
Units: bit, byte, kilobit, kilobyte, megabit, megabyte, gigabit, gigabyte, terabyte, petabyte
Bit: b, Kbps, Mbps (lowercase = bits)
Byte: B, KB, MBps (uppercase = bytes)
5. Volume (unitflex.vol)
Units: ml, cl, dl, l, hl, m3, tsp, tbsp, fl oz, cup, pt, qt, gal, in3, ft3, yd3
Supports both metric and imperial units (short and full names)
6. Pressure (unitflex.press)
Units: pa, kpa, mpa, bar, mbar, psi, atm, mmhg, torr, inhg, hpa, dyne/cm²
Handles variations like “millibar”, “hectopascal”, etc.
7. Speed (unitflex.speed)
Units: m/s, km/h, km/s, mph, knot, ft/s, ft/min, in/s, in/min, cm/s, mm/s, mach
Flexible recognition of various spellings and capitalizations
8. Time (unitflex.time)
Units: ns, μs, us, ms, s, min, h, d, wk, mo, yr, decade, century, millennium
Includes local terms like “quarter”, “semester”, “generation” and more
Based on accurate average durations (e.g., 1 month = 30.44 days)
All units support both short and long forms.
The system automatically recognizes and converts them.
<-- There will be many useful converters to come, stay tuned! -->
Parameters Explained 🔧
Each convert() function accepts up to six parameters. The first three are required, the rest are optional for customizing the output.
-
value (required)
The numeric value to convert (int or float)
Example: 100, 3.14 -
fromUnit (required)
The original unit (short, long name, or symbol)
Example: cm, meter, °f -
toUnit (required)
The target unit (same format options as fromUnit)
Example: km, yard, kelvin -
precision (optional, default = 2)
Number of decimal digits in the result
Example: precision=3 → 12.345 -
format (optional, default = "tag")
Output style:
• "raw" → numeric only (ideal for calculations)
• "tag" → number + unit
• "verbose" → detailed explanation (e.g. "1 meter = 100 cm") -
delim (optional)
Adds a separator:
• True or "default" → comma: 1,000,000
• "." → dot: 1.000.000
• False → no separator
⚠️ Note on delim and format="raw
When using format="raw", the output is intended for further calculations. Therefore, even if delim is set to "default" or any custom separator, no separators will be applied — the result will be returned as a clean float, int, or Decimal without formatting. -
mode (optional, default = "standard")
• "standard" → default mode
• "engineering" → high-precision mode using decimal.Decimal
Notes 📌
- Units are case-insensitive. (Exception: data and speed units where b ≠ B, and formatting like m/s matters.)
- Flexible unit names. For example: "kg", "kilogram", "Kilograms" are all valid and automatically recognized.
- Smart rounding system ensures accurate output and avoids losing significance. Whole number results are automatically simplified.
- Delimiters improve large number readability (e.g., 1_000_000 or 1,000,000).
- Supports multiple output formats: raw number, tagged result (value + unit), or full verbose expression.
- Highly precise with engineering mode — ideal for technical/scientific usage.
- Clear error messages for invalid inputs or unsupported units.
- Easily extendable for custom units in future versions.
⚠️ Note on Engineering Mode Output:
When using mode="engineering", the result is returned as a high-precision Decimal object to ensure maximum accuracy.
Please note that Decimal values cannot be directly operated with float or int types in Python.
If you need to perform mathematical operations with the result, consider casting it manually:
float_result = float(result)
Another Information 🔍
This library is now available on PyPI. You can install it directly using pip install unitflex. Once installed, you can import and use the unit classes from the unitflex package. Each class provides a convert() method that accepts the value to be converted, the source unit, the target unit, the number of decimal digits, and the preferred output style.
After the installation with pip install unitflex, you can import and use this library by:
from unitflex import length, speed, time
# Convert 12 mach to km/h
a = speed.convert(12, "mach", "km/h")
# Convert 18 years to seconds using delim, prec and format paramaters
b = time.convert(18, "year", "second", delim="default", prec=2, format="tag")
# Converting 12.0504 nanometers to centimeters using `engineering mode` to obtain a highly accurate result — ideal for outputs with many decimal places.
c = length.convert(12.0504, "nm", "cm", format="raw", prec=12, mode="engineering")
The folder structure of this project is organized for clarity and scalability. The main package, unitflex, contains individual modules for each category of conversion (such as length.py, mass.py, data.py and temperature.py). In addition, there are directories for usage examples and test scripts, which help demonstrate the library's capabilities and ensure consistent performance through future updates.
Upcoming features planned for unitflex include conversion of area, volume, time or even conversion with scientific units such as electric current, thermodynamic, amount of substance, luminous intensity, frequency, and more. While external contributions are currently limited as the library is in its early development stage, feedback and suggestions are more than welcome and encouraged.
Unitflex is released under the MIT License, which allows you to freely use, modify, and distribute the library as long as the original license is included. For more information about usage, structure, and licensing, please refer to the LICENSE file included in this repository.
Project details
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 unitflex-0.8.0.tar.gz.
File metadata
- Download URL: unitflex-0.8.0.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b756779347adbfc426bc9ecea8744d71d11fe3943abb9143dee9ab29e3bf46fd
|
|
| MD5 |
87971a6645d10dff7f080f40a34531a1
|
|
| BLAKE2b-256 |
105dc16cf3145a7b09c5327d2128fee46d3fb0bf71c9c1049e96278caa779720
|
File details
Details for the file unitflex-0.8.0-py3-none-any.whl.
File metadata
- Download URL: unitflex-0.8.0-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01345cad789e119c3c41b25a37380483153408b573f1c250d83a8633641e6ea7
|
|
| MD5 |
c8219adc30ab0b0c6cf3443fcceead95
|
|
| BLAKE2b-256 |
4d89b9c1968d58f3b37c8cd1fabe6d52e30c9dddacf25a13c90a848b717bfe4d
|