Skip to main content

A tiny persistent key–value store built on SQLite.

Project description

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.

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

picapica-0.1.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

picapica-0.1.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file picapica-0.1.2.tar.gz.

File metadata

  • Download URL: picapica-0.1.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for picapica-0.1.2.tar.gz
Algorithm Hash digest
SHA256 08c5ab9366ca7e4a48e612200fdaeef04e6ba3a7020b30131eb3e1b4c100f853
MD5 776d69af45d79b1e7cf1f6bb4615a4a0
BLAKE2b-256 e26540dcc830f1fe0833bc74697eec2986ec05d5b24d1b04cb8ec9eefe7ffbd8

See more details on using hashes here.

File details

Details for the file picapica-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: picapica-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for picapica-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f81e900123b20b957a871218908096a0f8c0cde6da657b4de823a26a28ffed6e
MD5 ef76db7e633b0f10eb1321faf00fcec6
BLAKE2b-256 a24783ae45a24bad877fc18c91064a192fad52f8e592b154f50423b99aa3a317

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