Skip to main content

A lightweight Python package providing shelve-like persistent storage with zstd compression, SQLite transactions, multithreading support, and flexible serialization options.

Project description

Introduction

PyPI

This package functions similarly to Python’s built-in shelve but offers additional features such as:

  • Zstandard (zstd) compression for efficient storage ✅ DONE
  • SQLite-backed transactions to ensure data integrity ⚠️ TODO
  • Multiple serialization formats support: JSON, Pickle, and Pydantic models ✅ DONE

Features

  • Transparent compression using zstd
  • Atomic transactions with SQLite
  • Flexible serialization: choose between JSON, Pickle, or Pydantic serialization
  • Safe concurrent read/write operations in multithreaded environments
  • Easy-to-use API similar to Python's built-in shelve

Installation

pip install shelvez

Base Usage

import shelve_sqlite_zstd as shelve

db = shelve.open("any_db_path/your_db.db")
db["key"] = "value"
print(db["key"])
db.close()

Serialization (default is Pickle)

The default serialization method uses Pickle, with the Pickle data further compressed by zstd. For specific data types, you can also choose other serialization methods to achieve better version compatibility and reduce storage size.


with JSON-serializable dicts

import shelve_sqlite_zstd as shelve

# Use Json serializer
serializer = shelve.serializer.JsonSerializer()
db = shelve.open("any_db_path/your_db.db", serializer=serializer)

db["key"] = {"key":"value"}

with Pydantic model

from pydantic import BaseModel
import shelve_sqlite_zstd as shelve

class MyModel(BaseModel):
    value: str

# use Pydantic serializer
serializer = shelve.serializer.PydanticSerializer(MyModel)
db = shelve.open("any_db_path/your_db.db", serializer=serializer)

db["key"] = MyModel(value="value")

with Self Custom Serialization

To implement your own serialization method, you need to create a subclass of serializer.BaseSerializer and override the following two methods:

  1. serialize(self, obj) -> bytes: This method takes a Python object (obj) and returns its serialized form as bytes. Implement this method with your custom serialization logic.
  2. unserialize(self, data: bytes): This method takes the serialized bytes (data) and returns the original Python object by deserializing it.

Here is a template example:

from shelve_sqlite_zstd import serializer

class CustomSerializer(serializer.BaseSerializer):
    def serialize(self, obj) -> bytes:
        # Implement custom serialization logic here
        # Convert `obj` to bytes
        pass

    def unserialize(self, data: bytes):
        # Implement custom deserialization logic here
        # Convert bytes back to original object
        pass

Using zstd Compression Dictionary

After accumulating a sufficient amount of data, you can generate a custom zstd compression dictionary for your database by calling db.dict.optimize_database(). This function will also recompress the existing data using the newly created dictionary. When stored data shares similar structures or formats, a personalized zstd dictionary can greatly enhance compression efficiency, particularly for relatively small datasets. Typically, generating the dictionary after storing a few thousand samples yields good results.

⚠️ Warning: During the optimization process, do not perform any other read or write operations on the database to prevent data corruption or inconsistent states.

import shelve_sqlite_zstd as shelve

db = shelve.open("any_db_path/your_db.db")
db["key"] = "value"
# ... store more data as needed

# Generate and apply a custom zstd compression dictionary
db.dict.optimize_database()

db.close()

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

shelvez-0.2.1.tar.gz (34.6 kB view details)

Uploaded Source

Built Distribution

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

shelvez-0.2.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file shelvez-0.2.1.tar.gz.

File metadata

  • Download URL: shelvez-0.2.1.tar.gz
  • Upload date:
  • Size: 34.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.4

File hashes

Hashes for shelvez-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4a437450e73bd3d5921341b717f93f3047be12b87c1f21757c78b2612d50c5a4
MD5 0b8c90b09ac48340b6a5d5f6cd161268
BLAKE2b-256 fb7e283f87f4469aa28a3d36ecd85613de04f0e94bc0a26197e3e131ae8cc496

See more details on using hashes here.

File details

Details for the file shelvez-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: shelvez-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.4

File hashes

Hashes for shelvez-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8e8056e08acdc6795f1ea8e60df9b251455f32d8c0a3f65b9ba0bfdd2dc70cf
MD5 e4c7af32c503189db96a6a79ed5a8fbd
BLAKE2b-256 98f48b8da15d5eacdf3ccc8c6d36a44133a53093c0b5772c2647fcba9de40e63

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