Skip to main content

A collection of utilities for Python

Project description

My Common Python Utils

Python application Upload Python Package

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 logging
  • JsonCache - 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 requests
  • paramiko - SSH tunneling
  • psutil - System monitoring
  • memory-profiler - Memory profiling
  • pyshorteners - URL shortening
  • slack-sdk - Slack integration
  • discord-webhook - Discord integration
  • pytz - 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 environment
  • make install - Install production dependencies
  • make install_dev - Install development dependencies
  • make test - Run all tests
  • make lint - Run linting (black, mypy, pylint)
  • make format - Format code with black and isort
  • make 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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite
  6. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ryutils-9.0.11.tar.gz (37.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ryutils-9.0.11-py3-none-any.whl (43.0 kB view details)

Uploaded Python 3

File details

Details for the file ryutils-9.0.11.tar.gz.

File metadata

  • Download URL: ryutils-9.0.11.tar.gz
  • Upload date:
  • Size: 37.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for ryutils-9.0.11.tar.gz
Algorithm Hash digest
SHA256 6eb599d018391d65a60b6cd7c48b9249969ddb195e874c9578df85662cd29b08
MD5 bda6833d6ef1321fcb29f8311a5ee094
BLAKE2b-256 8098db1ababea778bb0e188a72e2b375124d3f185101f251d2b35a59bd8181a4

See more details on using hashes here.

File details

Details for the file ryutils-9.0.11-py3-none-any.whl.

File metadata

  • Download URL: ryutils-9.0.11-py3-none-any.whl
  • Upload date:
  • Size: 43.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for ryutils-9.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 3ec57243829ff2874f8750adae7a86354af4ed247ddc77bc9f7497ec4fe15f7a
MD5 effcf9d785ae2f4479abe94d2c386db1
BLAKE2b-256 a1a1f5affe7bbc95c5d63076b196567986303d7a3ea9265367016a578df8cb46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page