Common server utilities for Matrice.ai services
Project description
Matrice Common Library
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, video processing, and more.
๐ Quick Start
Installation
pip install --index-url https://test.pypi.org/simple/ matrice_common
Basic Usage
from matrice_common.rpc import RPC
from matrice_common.session import create_session
# Initialize RPC client
rpc = RPC(
access_key="your_access_key",
secret_key="your_secret_key"
)
# Make API requests
response = rpc.get("/v1/endpoint")
data = rpc.post("/v1/resource", payload={"key": "value"})
# Create a session
session = create_session(
access_key="your_access_key",
secret_key="your_secret_key"
)
# Create a project
project = session.create_classification_project(
name="My Project",
description="AI classification project"
)
โจ Key Features
๐ Authentication & Security
- Token-based authentication with automatic refresh
- Secure credential management
- Environment-based configuration (prod/staging/dev)
๐ RPC Client
- Synchronous and asynchronous HTTP methods
- Automatic token management
- Built-in error handling and retry logic
- Type-safe API interactions
๐ Streaming
- Unified Interface: Single API for Kafka and Redis streaming
- Async Support: Full async/await compatibility
- Metrics & Monitoring: Built-in performance tracking
- Auto-Reconnection: Resilient connection handling
๐ฅ Video Processing
- H.265 encoding and decoding
- Hardware acceleration support
- Stream processing capabilities
- Frame optimization algorithms
๐ง Utilities
- Comprehensive error logging (Sentry + Kafka)
- Error deduplication
- Automatic dependency installation
- Caching decorators
- Type hints throughout
๐ฆ Session & Compute Management
- Project lifecycle management
- Compute instance orchestration
- Cluster management
- Resource allocation
๐ Documentation
Comprehensive documentation is available in DOCUMENTATION.md, including:
- Installation Guide - Setup and requirements
- Authentication - Token management and security
- RPC Client - API communication
- Session Management - Project lifecycle
- Compute Management - Instance and cluster control
- Streaming - Kafka and Redis streaming
- Video Processing - H.265 encoding/decoding
- Frame Optimization - Intelligent transmission
- Error Handling - Logging and monitoring
- Utilities - Helper functions
- API Reference - Complete API docs
- Testing - Test suite information
- Examples - Code examples
๐ก Examples
Async API Calls
import asyncio
from matrice_common.rpc import RPC
async def fetch_data():
rpc = RPC(access_key="...", secret_key="...")
# Concurrent requests
results = await asyncio.gather(
rpc.get_async("/v1/users"),
rpc.get_async("/v1/projects"),
rpc.get_async("/v1/datasets")
)
return results
asyncio.run(fetch_data())
Streaming with Kafka
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}
Video Encoding
from matrice_common.video.h265_processor import encode_frame_h265, decode_frame_h265
from PIL import Image
# Load and encode frame
frame = Image.open("frame.jpg")
encoded = encode_frame_h265(frame, quality=23, preset="medium")
# Decode frame
decoded = decode_frame_h265(encoded)
decoded.show()
๐งช 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
๐๏ธ Project Structure
py_common/
โโโ src/matrice_common/ # Source code
โ โโโ rpc.py # RPC client
โ โโโ token_auth.py # Authentication
โ โโโ utils.py # Utilities and error handling
โ โโโ session.py # Session management
โ โโโ compute.py # Compute management
โ โโโ stream/ # Streaming modules
โ โ โโโ matrice_stream.py
โ โ โโโ kafka_stream.py
โ โ โโโ redis_stream.py
โ โโโ optimize/ # Frame optimization
โ โ โโโ cache_manager.py
โ โ โโโ frame_comparators.py
โ โ โโโ frame_difference.py
โ โ โโโ transmission.py
โ โโโ video/ # Video processing
โ โโโ h265_processor.py
โ โโโ h265_video_processor.py
โโโ tests/ # Test suite
โ โโโ conftest.py # Test fixtures
โ โโโ unit/ # Unit tests
โ โโโ integration/ # Integration tests
โโโ DOCUMENTATION.md # Comprehensive documentation
โโโ README.md # This file
โโโ setup.py # Package setup
โโโ pyproject.toml # Project configuration
โโโ pytest.ini # Pytest configuration
โโโ requirements-dev.txt # Development dependencies
๐ง Development
Setup Development Environment
# Clone repository
git clone <repository-url>
cd py_common
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install development dependencies
pip install -r requirements-dev.txt
# Install package in editable mode
pip install -e .
Code Quality
# Format code
black src/
# Lint code
flake8 src/
# Type checking
mypy src/
Building the Package
# Build package
python setup.py bdist_wheel sdist
# Build with PyArmor obfuscation (optional)
python setup.py build
# Skip obfuscation
SKIP_PYARMOR_OBFUSCATION=true python setup.py bdist_wheel
๐ Environment Variables
Core Configuration
| Variable | Description | Required | Default |
|---|---|---|---|
MATRICE_ACCESS_KEY_ID |
API access key | Yes | - |
MATRICE_SECRET_ACCESS_KEY |
API secret key | Yes | - |
ENV |
Environment (prod/staging/dev) | No | prod |
MATRICE_ACTION_ID |
Current action ID | No | - |
MATRICE_SESSION_ID |
Current session ID | No | - |
SKIP_PYARMOR_OBFUSCATION |
Skip code obfuscation | No | false |
Error Logging & Deduplication
| Variable | Description | Required | Default |
|---|---|---|---|
MATRICE_ERROR_DEDUPLICATION_ENABLED |
Enable error deduplication | No | true |
MATRICE_ERROR_CACHE_TTL_SECONDS |
Deduplication time window (seconds) | No | 900 (15 min) |
MATRICE_ERROR_CACHE_MAX_SIZE |
Max unique errors to track | No | 1000 |
Setting Environment Variables
# Linux/Mac
export MATRICE_ACCESS_KEY_ID="your_access_key"
export MATRICE_SECRET_ACCESS_KEY="your_secret_key"
export ENV="staging"
# Windows PowerShell
$env:MATRICE_ACCESS_KEY_ID="your_access_key"
$env:MATRICE_SECRET_ACCESS_KEY="your_secret_key"
$env:ENV="staging"
# Python
import os
os.environ['MATRICE_ACCESS_KEY_ID'] = 'your_access_key'
os.environ['MATRICE_SECRET_ACCESS_KEY'] = 'your_secret_key'
๐ Module Overview
Core Modules
| Module | Purpose | Key Features |
|---|---|---|
| rpc.py | API Communication | Sync/Async HTTP, auto-auth, retry logic |
| token_auth.py | Authentication | Token management, auto-refresh |
| utils.py | Utilities | Error logging, caching, helpers |
| session.py | Session Management | Project lifecycle, CRUD operations |
| compute.py | Compute Management | Instance/cluster management |
Streaming Modules
| Module | Purpose | Backend |
|---|---|---|
| matrice_stream.py | Unified Streaming | Kafka/Redis |
| kafka_stream.py | Kafka Streaming | Apache Kafka |
| redis_stream.py | Redis Streaming | Redis Streams |
Video & Optimization
| Module | Purpose | Features |
|---|---|---|
| h265_processor.py | Video Processing | H.265 encode/decode |
| frame_comparators.py | Frame Comparison | SSIM, perceptual hashing |
| frame_difference.py | Difference Detection | Change detection |
| transmission.py | Optimized Transfer | Smart transmission |
๐ค Contributing
We welcome contributions! Please follow these guidelines:
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Write tests for new functionality
- Ensure tests pass:
pytest - Format code:
black src/ - Submit pull request
Contribution Guidelines
- Maintain test coverage above 80%
- Follow PEP 8 style guide
- Add type hints to all functions
- Write clear docstrings (Google style)
- Update documentation for new features
๐ License
This project is licensed under the MIT License - see the LICENSE.txt file for details.
๐ Acknowledgments
- Built for Matrice.ai services
- Uses industry-standard libraries (requests, aiohttp, Kafka, Redis)
- Inspired by modern Python best practices
๐ Support
- Documentation: DOCUMENTATION.md
- Issues: GitHub Issues
- Email: support@matrice.ai
๐บ๏ธ Roadmap
- Additional streaming backends (RabbitMQ, NATS)
- Enhanced video codecs (H.264, VP9, AV1)
- GraphQL support
- WebSocket streaming
- Real-time metrics dashboard
- CLI tool for common operations
Made with โค๏ธ by the Matrice.ai Team
Last Updated: 2025-01-30 | Version: 0.0.2
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 matrice_common-0.1.49.tar.gz.
File metadata
- Download URL: matrice_common-0.1.49.tar.gz
- Upload date:
- Size: 125.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
411ef77440e16e84364d18747da67b65e67881baf34338979feb62841d641f04
|
|
| MD5 |
25f5a6a3faa11b2d6467caedd8d9b9fd
|
|
| BLAKE2b-256 |
41edc69f62a321c4f3390bae62bd196682a49419b3c0339bf93d454b59289db1
|
File details
Details for the file matrice_common-0.1.49-py3-none-any.whl.
File metadata
- Download URL: matrice_common-0.1.49-py3-none-any.whl
- Upload date:
- Size: 133.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3316b4234219f55ad9d607e421bb02fe7d17d915cda2a1a4206bffd6bc77b209
|
|
| MD5 |
f3f915aa528d6eca4a8b260970135d75
|
|
| BLAKE2b-256 |
f0d9a94af9c588146928f8a0593acb143333d7290301f6f066b2094eccc88a89
|