How to Zip and Encode a Dictionary to String and Back in Python
If you have a Python dictionary, and want to encode it as a string and zip it to save space, perhaps for passing a dictionary through as an environment variable or similar, then you can do the following Zip then Encode / Decode then Unzip Functions import json, gzip, base64 from io import BytesIO def _zip_then_encode(data: dict) -> str: """Gzip and base64 encode a dictionary""" if type(data) != dict: raise TypeError("data must be a dictionary") compressed = BytesIO() with gzip....