Skip to main content

Shelve-like dict using sqlalchemy as a backend, and lazy scheduler for resuming tasks

Project description

test codecov github Python version license: GPL v3

API documentation Downloads PyPI - Downloads

shelchemy - Dict-like (shelve-like) storage wrapper for any DBMS (SQLAlchemy)

Python installation

from package

# Set up a virtualenv. 
python3 -m venv venv
source venv/bin/activate

# Install from PyPI
pip install shelchemy

from source

git clone https://github.com/shelchemy/shelchemy
cd shelchemy
poetry install

Examples

This library is more useful when used along with hdict. Here are some possible usages by itself.

Scheduling jobs.

from time import sleep

from shelchemy.scheduler import Scheduler

# Jobs can be distributed across multiple computers/networks.
names1 = ["a", "b"]
names2 = ["c"]
names3 = ["d", "e"]
storage = {}
# `storage` can be: shelve; URI pointing to a database; or, any dict-like object.
#   Example of a local database: storage="sqlite+pysqlite:////tmp/sqla-test.db"
#   Example of a remote database: storage="mysql+pymysql://user1:password1@hosh.page/db1"
for name in Scheduler(storage, timeout=10) << names1 << names2 << names3:
    print(f"Processing {name}")
    sleep(0.1)
    print(f"{name} processed!")
"""
2023-11-13 00:03:40.115637 'a' is new, starting
Processing a
a processed!
2023-11-13 00:03:40.316130 'a' done
2023-11-13 00:03:40.326101 'b' is new, starting
Processing b
b processed!
2023-11-13 00:03:40.526549 'b' done
2023-11-13 00:03:40.534599 'c' is new, starting
Processing c
c processed!
2023-11-13 00:03:40.735062 'c' done
2023-11-13 00:03:40.745734 'd' is new, starting
Processing d
d processed!
2023-11-13 00:03:40.946152 'd' done
2023-11-13 00:03:40.957263 'e' is new, starting
Processing e
e processed!
2023-11-13 00:03:41.157701 'e' done
"""

Persistent dict.

from shelchemy import sopen
from shelchemy.cache import Cache

d = Cache("sqlite+pysqlite:////tmp/sqla-test.db")
d["x"] = 5
d["b"] = None
print(d["x"], d["b"])
"""
5 None
"""
try:
    d["xxx"]
except KeyError as m:
    print(m)
"""
'xxx'
"""
for k, v in d.items():
    print(k, v)
print("x" in d)
"""
872d417d62b78366a71ab9fee25f14dc None
aed0339093d97301965a4e23dac3424a b'only bytes when autopack=False'
a b'by'
x 5
b None
True
"""
del d["x"]
print("x" in d)
"""
False
"""
print(d)
"""
{'872d417d62b78366a71ab9fee25f14dc': None, 'aed0339093d97301965a4e23dac3424a': b'only bytes when autopack=False', 'a': b'by', 'b': None}
"""
# Using a context manager.
with sopen() as db:
    print("x" in db)
    print(db)

    db["x"] = b"asd"
    print(db)
    print(db["x"] == b"asd")
    print("x" in db)
    print(db.x == b"asd")

    del db["x"]
    print("x" in db)

    db["any string key longer than 40 characters will be hashed depending if the DBMS backend is used"] = None
    print(db)
"""
False
{}
{'x': b'asd'}
True
True
True
False
{'872d417d62b78366a71ab9fee25f14dc': None}
"""

Grants

This work was partially supported by Fapesp under supervision of Prof. André C. P. L. F. de Carvalho at CEPID-CeMEAI (Grants 2013/07375-0 – 2019/01735-0).

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

shelchemy-2.240219.2.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

shelchemy-2.240219.2-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file shelchemy-2.240219.2.tar.gz.

File metadata

  • Download URL: shelchemy-2.240219.2.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.6 Linux/5.15.0-91-generic

File hashes

Hashes for shelchemy-2.240219.2.tar.gz
Algorithm Hash digest
SHA256 c1a990ce25c90d38e4621e9339109363f7e5791d60c36cd47622fbefab3bdb3b
MD5 d1505ba325e1bbad53d1a5d9bfacd3d7
BLAKE2b-256 477d83aebc68d63a33b65cc6590ea560b019cda2cafafb61776df4cd0f32acbf

See more details on using hashes here.

File details

Details for the file shelchemy-2.240219.2-py3-none-any.whl.

File metadata

  • Download URL: shelchemy-2.240219.2-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.6 Linux/5.15.0-91-generic

File hashes

Hashes for shelchemy-2.240219.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a680a829e3e0eda6143c09abc23dcbcac2a403501316e97ea609e216221d7a43
MD5 5d7ac9c4a0c0e95c02b01bb190549658
BLAKE2b-256 ed358001617f631a68522a154e7644e147cc520cb36906eda4ee3f5dd7c1ffef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page