Skip to main content

A flexible unit conversion tool with support for custom groups, types, aliases, and date/time conversions.

Project description

ConvUnit

Python License: MIT Code style: Black


Description

A flexible unit conversion tool written in Python that supports multiple categories (length, mass, temperature, time, volume, area, and speed), with support for custom units, aliases, base unit changes, and conversion history.

Three ways to use it:

  • Interactive mode (guided menu)
  • Command Line (CLI)
  • Python API

Features

  • Convert — Convert units between different categories, including complex date and time conversions with multiple input formats
  • History — View conversion history from the last 3 days
  • Groups — List all available unit groups
  • Types — View all unit types (and their aliases) of a specific group (or all groups)
  • Base — View the base unit of a group (or all groups)
  • Manage Groups — Add or remove custom unit groups
  • Manage Types — Add or remove unit types within a group
  • Aliases — Create or remove aliases for unit types
  • Change Base — Change the base unit of any group
  • Reset Data — Reset all custom data (groups, types, aliases, etc.) back to original state

Table of Contents


Installation

ConvUnit has one runtime dependency: platformdirs>=4.0 (used to locate the user configuration directory in a cross-platform way). All other code uses only Python's standard library.

Normal Installation (recommended)

pip install convunit

After installation, all modes work out of the box:

  • Interactive mode: python -m convunit
  • CLI mode: convunit convert length meters feet 10
  • Python API: from convunit import Converter

Note for developers: If you cloned the repository but prefer not to install the package with pip install -e ., you can still run it with python -m src.convunit.


Quick Start

For Normal Users

pip install convunit

# Interactive mode
python -m convunit

# CLI mode (example)
python -m convunit convert length meters feet 10

# Alternative: use the 'convunit' command directly
convunit convert length meters feet 10

# API mode
from convunit import Converter
converter = Converter()

Note: To launch interactive mode after installation, use python -m convunit. Running just convunit (without any subcommand) will show the help message instead of starting interactive mode.

For Developers

If you want to modify the source code, add new features, fix bugs, or contribute to the project, you need to clone the repository and install it in editable mode (recommended):

# Clone the repository
git clone https://github.com/XanD0K/ConvUnit.git
cd ConvUnit

# Install in editable mode (recommended)
pip install -e .

Then you can use the project normally as shown above.


Usage

ConvUnit supports three ways of use:

1. Interactive Mode

Run the following command:

python -m convunit

Just enter the program with the command above and follow the on-screen instructions.
Type quit (q) or exit (e) to exit.

2. CLI Mode

You can run commands directly from the terminal:

python -m convunit groups
python -m convunit convert length meters feet 10
python -m convunit history --limit 20
python -m convunit types length --all

Most commands support the --help flag to see available options and flags:

python -m convunit --help
python -m convunit history --help

3. API Mode (Python Library)

from convunit import Converter

converter = Converter()

# Examples
print(converter.groups())
print(converter.convert("length", "meters", "feet", 10))
print(converter.history(limit=10))

Note: Do not run python src/convunit/main.py directly (it will cause import errors due to relative imports). Use python -m src.convunit or install the package instead. Note: For complete usage instructions, detailed command references and all Date & Time formats, see USAGE.md.

Date & Time Conversion

ConvUnit has powerful support for date and time conversions with flexible input formats, including differences between dates, months, and times, plus summing multiple units.

See USAGE.md for the full list of supported formats and detailed examples.

Examples

Interactive Mode:

python -m convunit
convert
time
5 years 10 months 10 days 8 hours seconds

CLI Mode

python -m convunit convert time 5 years 10 months 10 days hours

API Mode

result = converter.convert("time", time_input="5 years 10 months 10 days 8 hours seconds")
print(result)

Project Structure

convunit/
├── src/
│   └── convunit/                  # Main package
│       ├── data/                  # JSON data files
│       │   ├── base_units.json
│       │   ├── conversion_log.json
│       │   ├── month_aliases.json
│       │   ├── month_days.json
│       │   ├── original_units.json
│       │   ├── units.json
│       │   └── unit_aliases.json
|       |
│       ├── __init__.py
│       ├── __main__.py
│       ├── main.py
│       ├── api.py                 # Public API interface (Converter class)
│       ├── cli.py                 # Command-line interface
│       ├── interactive.py         # Interactive mode
│       ├── aliases.py
│       ├── change_base.py
│       ├── convert.py
│       ├── convert_time.py
│       ├── data_manager.py        # JSON load/save logic
│       ├── data_models.py         # Data classes and validation
│       ├── display.py
│       ├── groups.py
│       ├── manage_types.py
│       ├── time_utils.py
│       ├── utils.py
│       └── errors.py              # Custom exceptions
│
├── tests/                         # Unit tests
├── CHANGELOG.md
├── DEVLOG.md
├── README.md
├── TODO.md
├── USAGE.md
└── requirements.txt

Design Choices

  • Modular Architecture: The project is divided into multiple focused modules instead of a single large file. This greatly improves readability, maintainability, and separation of concerns.

  • Data-Driven Approach: All unit definitions, aliases, and base units are stored in JSON files. This allows easy extension and modification without changing the source code.

  • Custom Exception Hierarchy: A well-defined exception system (AppError and its subclasses) was implemented to provide clear and meaningful error messages.

  • Three Access Modes: The program supports Interactive, CLI, and API modes simultaneously, giving users flexibility depending on their needs.

  • Specialized Time Handling: Dedicated modules (time_utils.py and convert_time.py) were created to properly manage the complexity of dates, months, leap years, and multiple input formats, keeping the main conversion logic clean.

  • API-First Mindset: Even though the project started as a simple tool, the code was designed from the beginning to also work as a reusable Python library.


Contributing

Contributions are welcome! If you want to contribute to ConvUnit, please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Test your changes thoroughly
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

Feel free to open an Issue for bug reports, feature requests, or questions.


License

This project is licensed under the MIT License.
See the LICENSE file for details.

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

convunit-1.1.0.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

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

convunit-1.1.0-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

Details for the file convunit-1.1.0.tar.gz.

File metadata

  • Download URL: convunit-1.1.0.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for convunit-1.1.0.tar.gz
Algorithm Hash digest
SHA256 80a8d14b95922ccc9ead9631bbb73e415d7d99a4035c92ab26ebd2b274e79ae2
MD5 a0c56934c5166fc1418116a1a48cbefa
BLAKE2b-256 2f4eb49facb088680e258b9d1e0fadc3f58a27314d1afdeb99fd41d99939c36f

See more details on using hashes here.

File details

Details for the file convunit-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: convunit-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for convunit-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a2996460727cc4b226c80d823c76696017d23df2e6f03e902418ab7ea1830ade
MD5 c1c35d6283258a2993a635ae6b1f3421
BLAKE2b-256 4d52b6ba1eca7f3617c78f2c3b6145ae38533c3f9d74c701ad1922d6674ddfbd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page