A collection of utilities for Python
Project description
My Common Python Utils
A comprehensive collection of Python utilities built over time for various projects. This library provides essential tools for HTTP requests, caching, logging, system monitoring, alerts, and more.
Installation
pip install ryutils
Features
🌐 HTTP Requests & Caching
RequestsHelper- Advanced HTTP client with retry logic, caching, and comprehensive loggingJsonCache- Lightweight JSON-based cache with expiration support- Request caching - Automatic caching of GET requests with configurable expiration
- Retry strategies - Exponential backoff for failed requests
- Request logging - Comprehensive logging of all HTTP requests and responses
📊 System Monitoring & Profiling
ResourceProfiler- CPU and memory usage profiling decorator- System stats - Real-time system resource monitoring
- Performance tracking - Function execution time and resource consumption
📝 Logging & Output
log- Advanced logging system with colored output and multiple handlers- Multi-threaded logging - Per-thread log files with configurable prefixes
- Verbose output - Configurable verbosity levels for different components
- CSV logging - Structured logging to CSV files
🚨 Alerting System
- Slack alerts - Send notifications to Slack channels
- Discord alerts - Send notifications to Discord webhooks
- Mock alerts - Testing and development alert system
- Alert factory - Unified interface for different alert types
📱 SMS & Communication
- TextBelt SMS - Send SMS messages via TextBelt API
- Multi-region support - US, Canada, and international SMS support
🔧 Utilities
- Dictionary utilities - Advanced dictionary manipulation and comparison
- Path utilities - File and directory path helpers
- URL shortening - TinyURL integration for URL shortening
- Text processing - Text chunk reading and processing utilities
- Format utilities - Data formatting and conversion helpers
🔒 Security & Networking
- SSH tunneling - Modern SSH tunnel implementation using paramiko
- Proxy capture - Mitmproxy integration for request/response capture
- Header extraction - Extract headers and cookies from captured requests
🧵 Concurrency
- Worker threads - Queue-based worker thread implementation
- Thread-safe operations - Thread-safe caching and logging
Quick Start
HTTP Requests with Caching
from ryutils.requests_helper import RequestsHelper
from ryutils.verbose import Verbose
# Create a requests helper with caching
helper = RequestsHelper(
base_url="https://api.example.com",
verbose=Verbose(),
cache_expiry_seconds=3600, # 1 hour cache
cache_file=Path("cache.json")
)
# GET request (automatically cached)
data = helper.get("/users", {"page": 1})
# POST request (clears cache for the endpoint)
result = helper.post("/users", {"name": "John"})
System Resource Profiling
from ryutils.system_stats import profile_function
@profile_function
def my_function():
# Your code here
return "result"
# Function execution will be automatically profiled
result = my_function()
Alerting
from ryutils.alerts.factory import AlertFactory
from ryutils.alerts.alert_types import AlertType
# Create alert factory
factory = AlertFactory()
# Send Slack alert
factory.send_alert(
AlertType.SLACK,
message="System is running smoothly",
webhook_url="https://hooks.slack.com/..."
)
# Send Discord alert
factory.send_alert(
AlertType.DISCORD,
message="Deployment completed",
webhook_url="https://discord.com/api/webhooks/..."
)
Logging
from ryutils import log
# Setup logging
log.setup(
log_dir="./logs",
log_level="INFO",
main_thread_name="main"
)
# Use colored logging
log.print_ok("Success message")
log.print_warn("Warning message")
log.print_fail("Error message")
log.print_bright("Info message")
Dictionary Utilities
from ryutils.dict_util import (
check_dict_keys_recursive,
patch_missing_keys_recursive,
flatten_dict,
safe_get
)
# Check for missing keys recursively
missing = check_dict_keys_recursive(template_dict, data_dict)
# Patch missing keys
patched = patch_missing_keys_recursive(template_dict, data_dict)
# Flatten nested dictionary
flat = flatten_dict(nested_dict)
# Safe dictionary access
value = safe_get(data, "key.subkey", default="default")
SMS Notifications
from ryutils.sms.pytextbelt import Textbelt
# Create SMS client
sms = Textbelt()
# Send SMS
recipient = sms.Recipient(
phone="+1234567890",
key="your-textbelt-key",
region="us"
)
sms.send(recipient, "Hello from Python!")
SSH Tunneling
from ryutils.modern_ssh_tunnel import ModernSSHTunnel
# Create SSH tunnel
tunnel = ModernSSHTunnel(
ssh_host="ssh.example.com",
ssh_port=22,
ssh_username="user",
ssh_pkey="path/to/private/key",
remote_host="remote.example.com",
remote_port=3306,
local_port=13306
)
# Start tunnel
tunnel.start()
# Use tunnel (e.g., connect to remote database on localhost:13306)
# ... your code ...
# Stop tunnel
tunnel.stop()
Configuration
Verbose Configuration
from ryutils.verbose import Verbose
verbose = Verbose(
requests=True, # Log HTTP requests
requests_url=True, # Log request URLs
requests_response=True, # Log responses
request_cache=True, # Log cache operations
alerts=True # Log alert operations
)
Cache Configuration
from ryutils.json_cache import JsonCache
cache = JsonCache(
cache_file=Path("cache.json"),
expiry_seconds=3600, # 1 hour expiration
verbose=Verbose()
)
Dependencies
The library uses minimal external dependencies:
requests- HTTP requestsparamiko- SSH tunnelingpsutil- System monitoringmemory-profiler- Memory profilingpyshorteners- URL shorteningslack-sdk- Slack integrationdiscord-webhook- Discord integrationpytz- Timezone handling
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/droneshire/my-py-utils.git
cd my-py-utils
# Create virtual environment
make init
# Install development dependencies
make install_dev
# Run tests
make test
# Run linting
make lint
Available Make Commands
make init- Initialize virtual environmentmake install- Install production dependenciesmake install_dev- Install development dependenciesmake test- Run all testsmake lint- Run linting (black, mypy, pylint)make format- Format code with black and isortmake clean- Clean up temporary files
Testing
The library includes comprehensive tests for all major components:
# Run all tests
make test
# Run specific test module
make test TESTMODULE=dict_util_test
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For questions, issues, or contributions, please use the GitHub Issues page.
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 ryutils-9.0.12.tar.gz.
File metadata
- Download URL: ryutils-9.0.12.tar.gz
- Upload date:
- Size: 38.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5370622153d8f1a2697a973b8e1a327e8959bbac68e8b88b9e03f84d75f5754
|
|
| MD5 |
ecfeb39ea1ee6b7c1f6377f99f4c9f69
|
|
| BLAKE2b-256 |
a47cc8c21fd49fc7d0b158969399c9d32d73e503052fb612c339dca9f04fe8af
|
File details
Details for the file ryutils-9.0.12-py3-none-any.whl.
File metadata
- Download URL: ryutils-9.0.12-py3-none-any.whl
- Upload date:
- Size: 43.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84ea62fa55fa5e98859f848fcf8877ab6ab94b4b8658d7d53a8aa41b965e556d
|
|
| MD5 |
ba94fe22e49ae1e1a365da23afb7c2ca
|
|
| BLAKE2b-256 |
9b5f88cbb85502cc503523e0471763530ecea9232b50af58bc93b453fea56b63
|