Python library for formatting addresses according to country-specific rules
Project description
Python Address Formatter
A comprehensive Python library for formatting addresses according to country-specific templates and rules. Built on the OpenCageData address-formatting templates, this library provides robust support for international address formatting.
Features
- Country-Specific Formatting: Format addresses according to country-specific rules and templates
- Component Normalization: Normalize address components with alias support and key conversion
- Abbreviation Support: Apply language-specific abbreviations to address components
- Template Management: Automatic conversion of YAML templates to JSON for runtime efficiency
- Plugin System: Extensible architecture with plugin support
- Event System: Event-driven architecture for flexible processing
- Machine Learning Integration: Component prediction and extraction from unstructured text
- API Support: FastAPI-based REST API for address formatting
- Monitoring: Prometheus metrics for monitoring performance and errors
- Asynchronous Processing: Support for async/await and batch processing
- CLI Tool: Command-line interface for formatter access
- Type System: Comprehensive type hints and Pydantic models
Installation
Basic Installation
pip install pyaddress
Installation with Extra Features
# Install with API support
pip install pyaddress[api]
# Install with async support
pip install pyaddress[async]
# Install with ML support
pip install pyaddress[ml]
# Install with optimization support
pip install pyaddress[optimize]
# Install with all features
pip install pyaddress[api,async,ml,optimize]
Quick Start
Format an Address
from address_formatter import format_address
# Format an address
formatted = format_address({
"houseNumber": "123",
"road": "Main St",
"city": "Anytown",
"state": "CA",
"countryCode": "US",
"postcode": "12345"
})
print(formatted)
# Output: 123 Main St
# Anytown CA 12345
Format with Options
# Format with options
formatted = format_address({
"houseNumber": "123",
"road": "Main Street",
"city": "Anytown",
"state": "California",
"countryCode": "US",
"postcode": "12345"
}, {
"abbreviate": True,
"append_country": True,
"output": "array"
})
print(formatted)
# Output: ['123 Main St', 'Anytown CA 12345', 'United States']
Async Processing
import asyncio
from address_formatter import format_address_async, format_batch_async
async def main():
# Format a single address asynchronously
formatted = await format_address_async({
"houseNumber": "123",
"road": "Main St",
"city": "Anytown",
"state": "CA",
"countryCode": "US"
})
print(formatted)
# Process a batch of addresses asynchronously
addresses = [
{
"houseNumber": "123",
"road": "Main St",
"city": "Anytown",
"state": "CA",
"countryCode": "US"
},
{
"houseNumber": "456",
"road": "High St",
"city": "Othertown",
"state": "NY",
"countryCode": "US"
}
]
results = await format_batch_async(addresses)
print(results)
asyncio.run(main())
Command-Line Interface
The library includes a command-line interface for easy access to formatting functionality:
# Format a single address
address-formatter format --input address.json --abbreviate
# Process a batch of addresses
address-formatter batch --input addresses.csv --output formatted.csv --format csv
# Run the API server
address-formatter server --port 8000
# Show formatter statistics
address-formatter stats
API Server
Start the API server with:
# Using the CLI
address-formatter server
# Or directly with Python
python -m address_formatter.api.server
Once running, you can access the API at http://localhost:8000 and view the API documentation at http://localhost:8000/docs.
API Endpoints
- POST /format: Format a single address
- POST /batch: Format multiple addresses
- POST /validate: Validate address components
- GET /health: Check API health
- GET /metrics: Get Prometheus metrics
- GET /stats: Get formatter statistics
Configuration
The formatter can be configured through environment variables:
# Address formatter configuration
export ADDRESS_TEMPLATE_DIR=/path/to/templates
export CACHE_SIZE=1000
export DEFAULT_LANG=en
# API configuration
export API_HOST=0.0.0.0
export API_PORT=8000
export API_RATE_LIMIT=100
# Performance configuration
export ENABLE_ASYNC=true
export ENABLE_JIT=false
export THREAD_POOL_SIZE=4
Docker Support
You can run the address formatter in a Docker container:
# Build the Docker image
docker build -t address-formatter .
# Run the container
docker run -p 8000:8000 address-formatter
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- OpenCageData for the address-formatting templates
- All contributors to the project
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 py_address_formatter-0.1.0.tar.gz.
File metadata
- Download URL: py_address_formatter-0.1.0.tar.gz
- Upload date:
- Size: 144.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa343bc55bd37b2e0f6045126597eb67e9f1282fac3350547d4e2a7aba464469
|
|
| MD5 |
29a73be4ec9623c15056f19d5a26c41a
|
|
| BLAKE2b-256 |
8b543a99bed4dec0b07ff44441ccd6623d40fd4c7bd11e62391db883d24e262c
|
File details
Details for the file py_address_formatter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: py_address_formatter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 82.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2b91cea0269a42354940cff6440a4f6dc82d236593fc02a46051b1015fce54d
|
|
| MD5 |
d0a33f9240cee6f60f8fcf3776f733af
|
|
| BLAKE2b-256 |
f698dd04493177ab5df930c40e9005cc61500d56d0856fbb5bea5bfa66bd6e04
|