Skip to main content

filedgr-pkg-utils

Build Status Coverage Python Version PyPI version License

A comprehensive, production-ready Python utility toolkit for modern distributed systems, Web3 integrations, and data-heavy microservices.

filedgr-pkg-utils provides a unified, protocol-driven API for cryptography, resilient networking, deterministic data serialization, compression, and structured logging.

📦 Installation

Install the base package (extremely lightweight, ideal for AWS Lambda):

pip install filedgr-pkg-utils

Install with optional image perceptual hashing support (includes Pillow and ImageHash):

pip install "filedgr-pkg-utils[images]"

🚀 Prominent Features & Examples

1. Resilience & Aspect-Oriented Hooks

Stop writing try/except blocks with time.sleep(). Stack our context-aware decorators to build bulletproof network calls and clean up your business logic.

from filedgr_pkg_utils.resilience.decorators import retry, circuit_breaker, fallback
from filedgr_pkg_utils.hooks.post_action import post_action

def audit_log(result, *args, **kwargs):
    print(f"Audit: Successfully fetched data {result}")

def serve_stale_data(*args, **kwargs):
    return {"status": "stale", "data": []}

@post_action(callback=audit_log)
@fallback(fallback_function=serve_stale_data)
@circuit_breaker(failure_threshold=5, recovery_timeout_seconds=30)
@retry(max_attempts=3, backoff_multiplier=2.0)
async def fetch_web3_data(node_url: str):
    # This function is completely protected.
    # It will retry on failure, trip a circuit breaker if the node is down,
    # serve stale data if all else fails, and log the result when successful.
    pass

2. Unified Compression

A single, clean protocol for zip, gzip, zstd, and brotli. Easily swap algorithms without changing your application code.

from filedgr_pkg_utils.compression import CompressionUtils, ZstdAlgorithm, ZipAlgorithm

# Swap from standard Zip to high-performance Zstd in one line
utils = CompressionUtils(ZstdAlgorithm(level=3))

# Compress bytes
compressed_data = utils.compress(b"Hello World")

# Compress files and directories
utils.compress_file("data.json", "data.json.zst")

3. Canonical Serialization & Cryptography

Generate deterministic, whitespace-free, and alphabetically sorted JSON payloads—perfect for creating verifiable hashes and blockchain signatures.

from filedgr_pkg_utils.canonicalization.canonical_json_mixin import CanonicalJsonMixin
from datetime import datetime, timezone

class BlockchainPayload(CanonicalJsonMixin):
    z_field: str
    a_field: int
    timestamp: datetime

payload = BlockchainPayload(
    z_field="last",
    a_field=1,
    timestamp=datetime(2023, 10, 25, 12, 30, 0, tzinfo=timezone.utc)
)

# Output is strictly deterministic: {"a_field":1,"timestamp":"2023-10-25T12:30:00.000Z","z_field":"last"}
json_string = payload.canonical_json()

4. Zero-Config Structured JSON Logging

Output logs as deterministic JSON for flawless Datadog or AWS CloudWatch ingestion. Fully integrates with Python's standard logging library.

from filedgr_pkg_utils.logging import LoggerFactory

# Automatically reads FILEDGR_LOGGING_ENABLED env var
logger = LoggerFactory.get_logger(__name__)

logger.info("Processing file", extra={"file_id": "ABC-123", "correlation_id": "999"})
# Output: {"timestamp": "2026-05-14T12:00:00.000Z", "level": "INFO", "logger": "__main__", "message": "Processing file", "file_id": "ABC-123", "correlation_id": "999"}

5. Perceptual Image Hashing (Optional)

Detect visually similar images, compression artifacts, and resized duplicates using Hamming distance.

from filedgr_pkg_utils.images.hashing import PHashAlgorithm, ImageHashUtils

utils = ImageHashUtils(PHashAlgorithm())

hash_orig = utils.hash_image("original.png")
hash_comp = utils.hash_image("compressed_artifact.jpg")

if utils.is_similar(hash_orig, hash_comp, max_distance=5):
    print("These images are visually similar!")

🛠️ Development & Testing

This project enforces high coverage and clean coding standards.

To run the test suite with coverage:

make test-coverage

To run the linter:

make lint

Download files

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

Source Distribution

filedgr_pkg_utils-0.8.4.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

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

filedgr_pkg_utils-0.8.4-py3-none-any.whl (33.9 kB view details)

Uploaded Python 3

File details

Details for the file filedgr_pkg_utils-0.8.4.tar.gz.

File metadata

  • Download URL: filedgr_pkg_utils-0.8.4.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for filedgr_pkg_utils-0.8.4.tar.gz
Algorithm Hash digest
SHA256 67febfa29f6caa788ae9809e2e11828a13ab6acdf0099e92b5ff7181d4de7536
MD5 659ce5b1557e3570c19a71b5af38306d
BLAKE2b-256 963ad776529f2e811d2a4d0273586bf7d23394eed114101a6c340e86f0d66dbb

See more details on using hashes here.

File details

Details for the file filedgr_pkg_utils-0.8.4-py3-none-any.whl.

File metadata

File hashes

Hashes for filedgr_pkg_utils-0.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3ec0074587e5a3ad9f5bed8bf2de337d0ebd1d3b16c96204f205fa0afca1a2bb
MD5 d77823974e27914d2492969c793256c7
BLAKE2b-256 035d6e0beb5e14494eaecd84908d73350c6c53f3602bf8acdb5132fb9867c938

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.8.4 This release

2 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