Skip to main content

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.

Release files for ryutils 9.0.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ryutils 9.0.19
File Size Uploaded
ryutils-9.0.19.tar.gz 39.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ryutils 9.0.19
File Interpreter ABI Platform
ryutils-9.0.19-py3-none-any.whl Python 3 none any Details

Total release size: 84.5 kB

Release files / ryutils-9.0.19.tar.gz

Download URL ryutils-9.0.19.tar.gz
Size 39.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a046293550e4f2993e80f9116685a0672024c215700f5e4ea63586e61cc819f1
BLAKE2b-256 checksum
How to use checksums
37d1e267c116826f290ad47587de0d1cc211392e6363a400125449741b8bc1a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / ryutils-9.0.19-py3-none-any.whl

Download URL ryutils-9.0.19-py3-none-any.whl
Size 44.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
acdcee9583fc9928185e5e857aaa5db5ea59361b6f3e17862df89a11938fe8b7
BLAKE2b-256 checksum
How to use checksums
9cc0acab99eb7109618cb9ae972abe36838fb7620d64d7639d782ffb772a73f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

9.0.19 This release

2 release files

9.0.14

2 release files

9.0.13

2 release files

9.0.9

2 release files

9.0.8

2 release files

9.0.7

2 release files

9.0.6

2 release files

9.0.5

2 release files

9.0.4

2 release files

9.0.3

2 release files

9.0.2

2 release files

9.0.1

2 release files

8.0.13

2 release files

8.0.12

2 release files

8.0.11

2 release files

8.0.10

2 release files

8.0.9

2 release files

8.0.8

2 release files

8.0.7

2 release files

8.0.6

2 release files

8.0.5

2 release files

8.0.4

2 release files

8.0.3

2 release files

7.3.0

2 release files

7.2.0

2 release files

7.0.1

2 release files

7.0.0

2 release files

6.0.2

2 release files

6.0.1

2 release files

6.0.0

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.2.0

2 release files

4.1.3

2 release files

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.5

2 release files

4.0.4

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.0.1

2 release files

2.1.0

2 release files

2.0.21

2 release files

2.0.12

2 release files

2.0.11

2 release files

2.0.10

2 release files

2.0.9

2 release files

2.0.8

2 release files

2.0.7

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page