A powerful Python library for unit conversions
Project description
Measurement Converter
A powerful Python library for handling various unit conversions with high precision and type safety. Perfect for applications requiring measurement conversions, scientific calculations, and engineering tools.
🌟 Key Features
- 📐 Multiple Measurement Types: Support for length, weight, volume, temperature, and more
- 🎯 High Precision Calculations: Configurable precision for all conversions
- 🔍 Type Hints: Full typing support for better development experience
- 🌐 Locale Support: Format results according to different locales
- ⚡ Batch Conversions: Convert multiple values at once
- 🧮 Formula Tracking: See the conversion formulas used
- 🛠️ Extensible: Easy to customize and extend
📦 Installation
pip install measurement_converter
🚀 Quick Start
from measurement_converter import MeasurementConverter
from measurement_converter.types import ConversionResult
# Simple length conversion
result = MeasurementConverter.convert(100, 'km', 'm')
print(f"Result: {MeasurementConverter.format_result(result)}")
# Output: Result: 100 km = 100000 m
# Temperature conversion
temp = MeasurementConverter.convert(32, 'F', 'C')
print(f"Temperature: {MeasurementConverter.format_result(temp)}")
# Output: Temperature: 32 F = 0 C
💡 Advanced Usage
🔄 Batch Conversion
# Convert multiple values at once
conversions = [
{'value': 1, 'from_unit': 'km', 'to_unit': 'm'},
{'value': 2.5, 'from_unit': 'kg', 'to_unit': 'lb'},
{'value': 30, 'from_unit': 'C', 'to_unit': 'F'}
]
results = MeasurementConverter.batch_convert(conversions)
for result in results:
print(MeasurementConverter.format_result(result))
🔍 Unit Validation
# Validate units with suggestions
validation = MeasurementConverter.validate_unit('kmh')
if not validation.is_valid:
print(f"Did you mean: {', '.join(validation.suggestions)}?")
🌐 Formatting Options
# Format results with different options
result = MeasurementConverter.convert(1, 'km', 'm')
formatted = MeasurementConverter.format_result(result, format_type='long')
print(formatted)
# Output: 1 kilometre is equal to 1000 metres
📋 Supported Units
Length
- Meters (m)
- Kilometers (km)
- Centimeters (cm)
- Millimeters (mm)
- Miles (mile)
- Yards (yard)
- Feet (foot)
- Inches (inch)
- Nautical Miles (nm)
- Micrometers (μm)
- Picometers (pm)
Weight
- Kilograms (kg)
- Grams (g)
- Milligrams (mg)
- Pounds (lb)
- Ounces (oz)
- Tons (ton)
- Stones (stone)
- Grains (grain)
Volume
- Liters (l)
- Milliliters (ml)
- Gallons (gal)
- Quarts (qt)
- Cups (cup)
- Fluid Ounces (floz)
- Tablespoons (tbsp)
- Teaspoons (tsp)
Temperature
- Celsius (C)
- Fahrenheit (F)
- Kelvin (K)
Area
- Square Meters (m2)
- Square Kilometers (km2)
- Hectares (ha)
- Acres (acre)
- Square Feet (sqft)
- Square Inches (sqin)
📋 Type Definitions
from dataclasses import dataclass
from typing import Optional, List
@dataclass
class ConversionResult:
from_value: float
from_unit: str
to_value: float
to_unit: str
formula: str
precision: int
@dataclass
class ValidationResult:
is_valid: bool
errors: Optional[List[str]] = None
suggestions: Optional[List[str]] = None
🔍 Error Handling
from measurement_converter import MeasurementConverter
from measurement_converter.errors import InvalidUnitError, ConversionError
try:
result = MeasurementConverter.convert(100, 'invalid', 'm')
except InvalidUnitError as e:
print(f"Invalid unit: {e}")
except ConversionError as e:
print(f"Conversion error: {e}")
🚀 Best Practices
- Always validate units before conversion
- Use proper unit symbols from the supported units list
- Handle errors appropriately
- Consider precision requirements for your specific use case
- Use batch conversions for multiple operations
- Cache common conversion results if needed
🛠️ Development
# Install dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run tests with coverage
pytest --cov=measurement_converter
# Run type checking
mypy src/measurement_converter
🤝 Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
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 measurement_converter-1.0.1.tar.gz.
File metadata
- Download URL: measurement_converter-1.0.1.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9da07c72c4e2dec54ee08e3b9e348e24088586aee768f9f843306970a5102de7
|
|
| MD5 |
33781094a40887310fe00c444cec2686
|
|
| BLAKE2b-256 |
f55344cb9a0afb6d0ad4ebef77564542cd43be4e5bc56cbc723a272aef2d2d9f
|
File details
Details for the file measurement_converter-1.0.1-py3-none-any.whl.
File metadata
- Download URL: measurement_converter-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df2a210c69a90812cce6d9d5abf72f277bc19284c49575bbb23fab761eff4925
|
|
| MD5 |
4d6330ff9d385fa487d1b7a9a7ae488c
|
|
| BLAKE2b-256 |
5050a4cfced130b7768dbd082402c5c5a1284c4a85889d5b226a093c0f2d403d
|