A Python library for simplified JSON/TXT Reading/Writing with Gzip compression
Project description
uni-gzip
A unified lightweight Python library for reading and writing gzip-compressed JSON and text files, and compressing data to bytes in memory.
Features
- Unified API: Simple functions (
readJsonGz,writeJsonGz,compressJSON, etc.) for all operations - Memory-efficient: Supports in-memory compression to bytes without writing to disk
- 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
strandPathobjects 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)
In-memory compression to bytes
from uni_gzip import compressJSON, compressTxt
# Compress data to gzip JSON bytes
data = {"key": "value", "number": 42}
gz_bytes = compressJSON(data)
# Now you can upload gz_bytes to Storage, etc.
# Compress text to gzip bytes
content = "Hello, World!"
gz_bytes = compressTxt(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)
compressJSON(data)
Compress data to a gzip-compressed JSON byte stream in memory.
Parameters:
data(Any): Data to serialize (must be JSON-serializable).
Returns:
bytes: Gzip-compressed bytes.
Raises:
TypeError: If data is not JSON-serializable.
Example:
from uni_gzip import compressJSON
data = {"key": "value", "number": 42}
gz_bytes = compressJSON(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)
compressTxt(content)
Compress text content to a gzip-compressed byte stream in memory.
Parameters:
content(str | Iterable[str]): Text content to compress. Can be:- A string: compressed as-is
- An iterable of strings: joined and compressed
Returns:
bytes: Gzip-compressed bytes.
Raises:
TypeError: If content is not a string or iterable of strings.
Example:
from uni_gzip import compressTxt
# Compress a string
gz_bytes = compressTxt("Hello, World!")
# Compress multiple lines
lines = ["Line 1", "Line 2", "Line 3"]
gz_bytes = compressTxt(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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file uni_gzip-1.1.1.tar.gz.
File metadata
- Download URL: uni_gzip-1.1.1.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c68a7902cd4fad360d202632aec57d0e921c2b075851ee2e15fcdd525c934711
|
|
| MD5 |
8b293bbba5da51f43d37fdb721708028
|
|
| BLAKE2b-256 |
35ad853f5d8c832164ad3995f12ed9545ccdc9fa64e9d6789805f50d283a0829
|
File details
Details for the file uni_gzip-1.1.1-py3-none-any.whl.
File metadata
- Download URL: uni_gzip-1.1.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa69b9cd629db15088399919b70c49791b51ba85528287ecb9a3948d09df25c5
|
|
| MD5 |
65cd8a3c93e8f04ba4e05c9e04ae3d24
|
|
| BLAKE2b-256 |
3f9a254c7a8dad460f993b0230b1e12e5e56d5d19290b4b512f2bd600936721e
|