Skip to main content

Pica: Persistent key-value storage

alt text

Overview

Pica pica: The Eurasian magpie. Known for collecting shiny things

The pica package is very similar to shelve (see docs here). Both enable key-value pairs to be stored on file without loading/saving entire dictionaries, which is super useful!

However, pica uses SQLite behind the scenes instead of DBM. This avoids some issues that shelve and dbm have with editing existing values causing runaway file bloat. See below for more info!

Usage

Installation: pip install picapica

Basic usage:

import pica

with pica.open("data.sqlite") as db:
    db["x"] = 1
    db["y"] = {"a": 42}

    print(db["x"])
    print("y" in db)
    print(len(db))

This saves data key-value pairs to the file data.sqlite.

To optimise the storage and reduce file size (e.g. after deleting or editing values), you can use vacuum:

with pica.open("data.sqlite") as db:
  db.vacuum()

Comparison with shelve

The main advantage of pica over shelve is how it copes with value rewrites for existing keys.

With shelve, repeatedly updating key-value pairs can cause file sizes to keep increasing much more than one might expect, potentially leading to huge but mostly empty files.

For example, if we run this script:

import os, shelve, pica

def kb(path): return os.path.getsize(path)//1024


print("=== shelve ===")
for i in range(100):
    with shelve.open("shelve") as db:
        db["data"] = list(range(i*100))  # keep changing size
print("shelve.db:", kb("shelve.db"), "KB")

print("\n=== pica ===")
for i in range(100):
    with pica.open("pica.sqlite") as db:
        db["data"] = list(range(i*100))
print("pica.sqlite:", kb("pica.sqlite"), "KB")

We get:

=== shelve ===
shelve.db: 508 KB

=== pica ===
pica.sqlite: 68 KB

Even though the contents of the files are the same: (a single list keyed by "data"), the file sizes are drastically different.

In fact, if we run the same script again, the shelve files keep growing:

=== shelve ===
shelve.db: 1388 KB

=== pica ===
pica.sqlite: 68 KB

This behaviour with shelve can become very inconvenient very quickly, often without anyone noticing. This is where our magpie pica shines.

Metadata

Release files for picapica 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for picapica 0.1.2
File Size Uploaded
picapica-0.1.2.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for picapica 0.1.2
File Interpreter ABI Platform
picapica-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / picapica-0.1.2.tar.gz

Download URL picapica-0.1.2.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
08c5ab9366ca7e4a48e612200fdaeef04e6ba3a7020b30131eb3e1b4c100f853
BLAKE2b-256 checksum
How to use checksums
e26540dcc830f1fe0833bc74697eec2986ec05d5b24d1b04cb8ec9eefe7ffbd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / picapica-0.1.2-py3-none-any.whl

Download URL picapica-0.1.2-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f81e900123b20b957a871218908096a0f8c0cde6da657b4de823a26a28ffed6e
BLAKE2b-256 checksum
How to use checksums
a24783ae45a24bad877fc18c91064a192fad52f8e592b154f50423b99aa3a317
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page