Skip to main content

A unified Python library for reading and writing gzip-compressed JSON and text files

Project description

uni-gzip

A unified lightweight Python library for reading and writing gzip-compressed JSON and text files with UTF-8 encoding and compact JSON format.

Features

  • Unified API: Four simple functions (readJsonGz, writeJsonGz, readTxtGz, writeTxtGz) for all operations
  • UTF-8 encoding: Automatic handling of UTF-8 encoding for international characters
  • Compact JSON format: Uses minimal JSON format (no spaces, no ASCII escaping) for efficient storage
  • Flexible text writing: Supports writing strings or iterables of strings
  • Type safety: Supports both str and Path objects for file paths
  • Error handling: Comprehensive exception hierarchy for precise error handling
  • Zero dependencies: Uses only Python standard library (gzip, json)

Installation

pip install uni-gzip

Quick Start

Reading and writing gzip-compressed JSON files

from uni_gzip import readJsonGz, writeJsonGz

# Write data to a gzip-compressed JSON file
data = {"key": "value", "number": 42, "list": [1, 2, 3]}
writeJsonGz("output.json.gz", data)

# Read a gzip-compressed JSON file
data = readJsonGz("output.json.gz")
print(data)

Reading and writing gzip-compressed text files

from uni_gzip import readTxtGz, writeTxtGz

# Write a string to a gzip-compressed text file
writeTxtGz("output.txt.gz", "Hello, World!")

# Write multiple lines from an iterable
lines = ["Line 1", "Line 2", "Line 3"]
writeTxtGz("output.txt.gz", lines)

# Read a gzip-compressed text file
content = readTxtGz("output.txt.gz")
print(content)

Using Path objects

from pathlib import Path
from uni_gzip import readJsonGz, writeJsonGz, readTxtGz, writeTxtGz

# Both str and Path objects are supported
path = Path("data.json.gz")
data = readJsonGz(path)

writeJsonGz(Path("output.json.gz"), {"example": "data"})
writeTxtGz(Path("output.txt.gz"), "Example text")

API Reference

readJsonGz(path)

Read a gzip-compressed JSON file.

Parameters:

  • path (str | Path): Path to the gzip-compressed JSON file.

Returns:

  • Any: Parsed JSON data (dict, list, or other JSON-serializable types).

Raises:

  • UniGzipJsonReadError: If reading fails due to:
    • File not found
    • Invalid gzip format
    • JSON parsing errors
    • I/O errors

Example:

from uni_gzip import readJsonGz

data = readJsonGz("data.json.gz")

writeJsonGz(path, data)

Write data to a gzip-compressed JSON file.

The file is written in compact JSON format (no spaces, no ASCII escaping) with UTF-8 encoding for efficient storage.

Parameters:

  • path (str | Path): Path to the output file.
  • data (Any): Data to serialize (must be JSON-serializable).

Raises:

  • UniGzipJsonWriteError: If writing fails due to:
    • Permission denied
    • Disk space issues
    • I/O errors
  • TypeError: If data is not JSON-serializable.

Example:

from uni_gzip import writeJsonGz

data = {"key": "value", "number": 42}
writeJsonGz("output.json.gz", data)

readTxtGz(path)

Read a gzip-compressed text file.

Parameters:

  • path (str | Path): Path to the gzip-compressed text file.

Returns:

  • str: Text content as a string.

Raises:

  • UniGzipTxtReadError: If reading fails due to:
    • File not found
    • Invalid gzip format
    • Encoding errors
    • I/O errors

Example:

from uni_gzip import readTxtGz

content = readTxtGz("data.txt.gz")

writeTxtGz(path, content)

Write content to a gzip-compressed text file.

The file is written with UTF-8 encoding. If content is a string, it is written as-is. If content is an iterable of strings, each string is written as a line (no newline is added automatically).

Parameters:

  • path (str | Path): Path to the output file.
  • content (str | Iterable[str]): Text content to write. Can be:
    • A string: written as-is
    • An iterable of strings: each string is written as a line

Raises:

  • UniGzipTxtWriteError: If writing fails due to:
    • Permission denied
    • Disk space issues
    • I/O errors
  • TypeError: If content is not a string or iterable of strings.

Example:

from uni_gzip import writeTxtGz

# Write a string
writeTxtGz("output.txt.gz", "Hello, World!")

# Write multiple lines
lines = ["Line 1", "Line 2", "Line 3"]
writeTxtGz("output.txt.gz", lines)

Exceptions

UniGzipError

Base exception class for all uni-gzip related errors.

Attributes:

  • message: Primary error message (required)
  • file_path: Path to the file that caused the error (optional)

UniGzipJsonError

Base exception class for uni-gzip JSON processing errors.

UniGzipJsonReadError

Raised when reading a gzip-compressed JSON file fails.

This exception indicates that an error occurred during the read operation, such as file not found, invalid gzip format, JSON parsing errors, or I/O errors.

Example:

from uni_gzip import readJsonGz, UniGzipJsonReadError

try:
    data = readJsonGz("nonexistent.json.gz")
except UniGzipJsonReadError as e:
    print(f"Error reading file: {e.message}")
    print(f"File path: {e.file_path}")

UniGzipJsonWriteError

Raised when writing a gzip-compressed JSON file fails.

This exception indicates that an error occurred during the write operation, such as permission denied, disk space issues, or I/O errors.

Example:

from uni_gzip import writeJsonGz, UniGzipJsonWriteError

try:
    writeJsonGz("/readonly/output.json.gz", {"data": "value"})
except UniGzipJsonWriteError as e:
    print(f"Error writing file: {e.message}")
    print(f"File path: {e.file_path}")

UniGzipTxtError

Base exception class for uni-gzip text processing errors.

UniGzipTxtReadError

Raised when reading a gzip-compressed text file fails.

This exception indicates that an error occurred during the read operation, such as file not found, invalid gzip format, encoding errors, or I/O errors.

Example:

from uni_gzip import readTxtGz, UniGzipTxtReadError

try:
    content = readTxtGz("nonexistent.txt.gz")
except UniGzipTxtReadError as e:
    print(f"Error reading file: {e.message}")
    print(f"File path: {e.file_path}")

UniGzipTxtWriteError

Raised when writing a gzip-compressed text file fails.

This exception indicates that an error occurred during the write operation, such as permission denied, disk space issues, or I/O errors.

Example:

from uni_gzip import writeTxtGz, UniGzipTxtWriteError

try:
    writeTxtGz("/readonly/output.txt.gz", "Hello, World!")
except UniGzipTxtWriteError as e:
    print(f"Error writing file: {e.message}")
    print(f"File path: {e.file_path}")

Requirements

  • Python >= 3.10

No external dependencies required. This package uses only Python standard library modules (gzip and json).

License

MIT License

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

uni_gzip-1.0.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

uni_gzip-1.0.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file uni_gzip-1.0.0.tar.gz.

File metadata

  • Download URL: uni_gzip-1.0.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for uni_gzip-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aa56499e92b2faa4794d5866bbe6b6c964a77124337aabc295d6fb27268f82ff
MD5 c4cab420ec9b71826edb8b9ca3d5d834
BLAKE2b-256 2ab5f990ddb77ef89853bde6f6f5d054f1f3c52aeb447cd5df481cb66660f1fd

See more details on using hashes here.

File details

Details for the file uni_gzip-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: uni_gzip-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for uni_gzip-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31d77752de527f1f5700913e0696788f7f1f014c9a6fbc2a6603c32fa651e0e7
MD5 437898a8cea43961c2575ae47f885898
BLAKE2b-256 4bb531d97259ec595f55d75e62779f1d881f195222f96e14c377d0e3732a9595

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