A modern, production-ready currency converter with live exchange rates
Project description
💱 fxconverter
A modern, Python package for currency conversion with live exchange rates.
✨ Features
- 🌍 Live Exchange Rates - Real-time currency conversion
- 💾 Smart Caching - Thread-safe caching with configurable TTL
- 🎯 High Precision - Decimal arithmetic for accurate calculations
- 🔌 Extensible - Pluggable provider system
- 🛡️ Type Safe - Full type hints support
- 📦 Zero Config - Works out of the box
- 🧪 Well Tested - Comprehensive test coverage
📦 Installation
pip install fxconverter
🚀 Quick Start
from fxconverter import CurrencyConverter
# Initialize converter
converter = CurrencyConverter()
# Convert currency
amount_eur = converter.convert(100, 'USD', 'EUR')
print(f"100 USD = {amount_eur} EUR")
# Get exchange rate
rate = converter.get_rate('USD', 'GBP')
print(f"1 USD = {rate} GBP")
# List supported currencies
currencies = converter.get_supported_currencies()
print(f"Supported: {len(currencies)} currencies")
📖 Usage Examples
Basic Conversion
from fxconverter import CurrencyConverter
converter = CurrencyConverter()
# Simple conversion
result = converter.convert(100, 'USD', 'EUR')
print(result) # Decimal('85.23')
Custom Configuration
from fxconverter import CurrencyConverter
# Configure cache TTL and precision
converter = CurrencyConverter(
cache_ttl=1800, # Cache for 30 minutes
precision=4 # 4 decimal places
)
result = converter.convert(100, 'USD', 'JPY')
print(result) # Decimal('11050.2500')
Disable Caching
# Fetch fresh rates every time
result = converter.convert(100, 'USD', 'EUR', use_cache=False)
Custom Provider
from fxconverter import CurrencyConverter
from fxconverter.providers import RateProvider
class MyCustomProvider(RateProvider):
def get_rates(self, base_currency: str):
# Your implementation
return {'EUR': 0.85, 'GBP': 0.73}
def get_supported_currencies(self):
return {'USD', 'EUR', 'GBP'}
converter = CurrencyConverter(provider=MyCustomProvider())
Error Handling
from fxconverter import CurrencyConverter
from fxconverter.exceptions import InvalidCurrencyError, RateFetchError
converter = CurrencyConverter()
try:
result = converter.convert(100, 'USD', 'INVALID')
except InvalidCurrencyError as e:
print(f"Invalid currency: {e}")
except RateFetchError as e:
print(f"Failed to fetch rates: {e}")
🔧 API Reference
CurrencyConverter
Main class for currency conversion.
Constructor
CurrencyConverter(
provider: Optional[RateProvider] = None,
cache_ttl: int = 3600,
precision: int = 2
)
Parameters:
provider: Exchange rate provider (defaults to ExchangeRateAPI)cache_ttl: Cache time-to-live in seconds (default: 3600)precision: Decimal places for results (default: 2)
Methods
convert(amount, from_currency, to_currency, use_cache=True)
Convert amount from one currency to another.
- Returns:
Decimal- Converted amount - Raises:
InvalidCurrencyError,RateFetchError
get_rate(from_currency, to_currency, use_cache=True)
Get exchange rate between two currencies.
- Returns:
Decimal- Exchange rate - Raises:
InvalidCurrencyError,RateFetchError
get_supported_currencies()
Get all supported currency codes.
- Returns:
set- Set of currency codes
clear_cache()
Clear the exchange rate cache.
🧪 Development
Setup Development Environment
# Clone repository
git clone https://github.com/NtohnwiBih/fxconverter.git
cd fxconverter
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install development dependencies
pip install -e ".[dev]"
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=fxconverter --cov-report=html
# Run specific test file
pytest tests/test_converter.py
Code Quality
# Format code
black src/ tests/
# Lint code
flake8 src/ tests/
# Type checking
mypy src/
Building Package
# Build distribution
python -m build
# Check distribution
twine check dist/*
Publishing to PyPI
# Test PyPI (recommended first)
twine upload --repository testpypi dist/*
# Production PyPI
twine upload dist/*
🤝 Contributing
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a 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
Contribution Guidelines
- Write tests for new features
- Maintain code coverage above 90%
- Follow PEP 8 style guidelines
- Update documentation
- Add entries to CHANGELOG.md
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Exchange rates provided by exchangerate-api.com
- Built with modern Python best practices
📞 Support
- 📫 Issues: GitHub Issues
- 📧 Email: mforbesintohn@gmail.com
- 💬 Discussions: GitHub Discussions
🗺️ Roadmap
- Support for additional rate providers
- Historical exchange rates
- Cryptocurrency support
- CLI tool
- Async API support
⭐ Star History
If you find this package useful, please consider giving it a star on GitHub!
Made with ❤️ by Ntohnwi Bih Mforbesi
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 fxconverter-1.0.1.tar.gz.
File metadata
- Download URL: fxconverter-1.0.1.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e06659da56b4db4645cb6c9c9f80f84eb859e4cc835f375776dc38441131f06
|
|
| MD5 |
9b3595d7bf17c7a3409bb55ef36f26d7
|
|
| BLAKE2b-256 |
924a8ed92178dbf8aeaac193124075f323ea1b9101084d931265d42321996dad
|
File details
Details for the file fxconverter-1.0.1-py3-none-any.whl.
File metadata
- Download URL: fxconverter-1.0.1-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a305669ec0ebf197f97bd3f48c3fe69d414f049dfbad0f9596b99cfab0cf04c3
|
|
| MD5 |
9f6c2194fac9479487d1f8f355cb3dd8
|
|
| BLAKE2b-256 |
6091ff47d11444b4320fcb41e72e9feeb10519735d36d446183b9b3a110c2c70
|