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

📊 Benchmark

Benchmark functions are defined in tests/test_shelve.py.

🔥 Performance Comparison

Test Case Min (ms) Max (ms) Mean (ms) StdDev (ms) Median (ms) OPS (ops/sec) Rounds
shelve_speed 443.62 459.37 450.19 5.96 450.10 2.22 5
shelvez_pickle_speed 237.18 243.08 240.01 2.54 239.53 4.17 5
shelvez_pydantic_speed 245.33 318.90 263.59 31.14 252.38 3.79 5
shelvez_json_speed 246.83 250.72 249.37 1.57 249.44 4.01 5

OPS = Operations Per Second (calculated as 1 / Mean)


🗂️ Database Size Comparison

File sizes are measured after writing the same number key-value data using each backend.

Backend File Size
shelve 380.00 kB
shelvez (Pickle) 312.00 kB
shelvez (JSON) 312.00 kB
shelvez (Pydantic) 308.00 kB

Smaller database files and faster write performance make shelvez a more efficient alternative to the standard shelve module.


Installation

pip install shelvez

Base Usage

import shelvez 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 shelvez 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 shelvez 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 shelvez 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 shelvez 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.3.1.tar.gz (35.8 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.3.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for shelvez-0.3.1.tar.gz
Algorithm Hash digest
SHA256 ed53493739cb5f477d93c82ea2a8097c4dcd1f0ac488c3e5c23f7e6d1f60c437
MD5 9257570e41614de6083ecce39c156ad9
BLAKE2b-256 f8e94bc6cb7c429114eb74f9f85ff64cde622e9ad45caddea29b1bfe2be66a79

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for shelvez-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8b73457f2f3bd9948fcaac496b213b48f9a184a8316143af5ec966213c1ab762
MD5 8f99e424e00ce542ef1b5684564fdb36
BLAKE2b-256 a3be89691b2681ea59c1975e2a57211687911b20f27a3d9528e991ed8495c937

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