matrice_common is a high-performance Python package providing reusable utilities for Matrice.ai services. It offers production-ready components for authentication, API communication, streaming, and more.
from matrice_common.stream.matrice_stream import MatriceStream, StreamType
# Create stream
stream = MatriceStream(
stream_type=StreamType.KAFKA,
access_key="...",
secret_key="..."
)
# Setup and use
stream.setup(topic_or_stream_name="my-topic")
stream.add_message({"data": "value", "timestamp": "2025-01-01T12:00:00Z"})
# Receive messages
message = stream.get_message(timeout=30)
if message:
print(f"Received: {message}")
stream.close()
Error Handling
from matrice_common.utils import log_errors, AppError, ErrorType, get_deduplication_config
# Configure deduplication (or use environment variables)
# export MATRICE_ERROR_DEDUPLICATION_ENABLED=true
# export MATRICE_ERROR_CACHE_TTL_SECONDS=1800 # 30 minutes
# Use service_name to properly track errors per service
@log_errors(service_name="my_service", raise_exception=False)
def process_data(data):
"""Automatically logs errors to Sentry and Kafka with deduplication."""
if not data:
raise ValueError("Data cannot be empty")
# Process data
return result
# Errors are automatically logged and deduplicated
result = process_data(my_data)
# Check deduplication config
print(get_deduplication_config())
# Output: {'enabled': True, 'ttl_seconds': 900, 'max_cache_size': 1000, 'current_cache_size': 0}
🧪 Testing
The library includes a comprehensive test suite with high coverage.
Running Tests
# Install development dependencies
pip install -r requirements-dev.txt
# Run all tests
pytest
# Run with coverage report
pytest --cov=src/matrice_common --cov-report=html
# Run specific test module
pytest tests/unit/test_rpc.py -v
# Run with verbose output
pytest -v --tb=short
Test Coverage
token_auth.py: 100% coverage
utils.py: 52% coverage
rpc.py: Comprehensive unit tests
Integration tests for all major workflows
View detailed coverage:
pytest --cov --cov-report=html
open htmlcov/index.html # View in browser