A library that automatically offloads large pickle-able objects stored in a dict/list to SQLite, reducing memory usage. It can be used with the same syntax as a regular dict/list.
Project description
English · 日本語 (Japanese)
swapcollection
A library that automatically offloads large objects stored in a
dict/list to SQLite, reducing memory usage. It can be used with
the same syntax as a regular dict/list.
Quick Start
pip install swapcollection
SwapDict
from swapcollection import SwapDict
# Values whose pickled size >= size_threshold are spilled to SQLite
d = SwapDict(size_threshold=100)
# Small values stay in-memory
d["name"] = "alice"
d.data # → {'name': 'alice'}
# Large values are transparently spilled to SQLite
d["blob"] = b"x" * 200
d.data # → {'name': 'alice', 'blob': 'swapdict_<uuid>'} (ID stored in memory)
# Retrieval works transparently — same as a normal dict
d["blob"] # → b'xxxxxxxx...'
# All standard dict methods work
d.update({"a": 1, "b": 2})
len(d) # → 4
list(d.keys()) # → ['name', 'blob', 'a', 'b']
d["name"] # → 'alice'
d.get("missing") # → None
d.pop("name") # → 'alice'
"name" in d # → False
d.setdefault("c", 99) # → 99
list(d.items())
# → [('blob', b'xxx...'), ('a', 1), ('b', 2), ('c', 99)]
# repr() also resolves spilled values
repr(d) # → "{'blob': b'xxx...', 'a': 1, 'b': 2, 'c': 99}"
# Class method
SwapDict.fromkeys(["x", "y"], 0)
# → {'x': 0, 'y': 0}
d.clear()
len(d) # → 0
SwapList
from swapcollection import SwapList
# Same threshold concept
xs = SwapList(size_threshold=100)
xs.append(42)
xs.append(b"y" * 200)
xs[0] # → 42
xs[1] # → b'yyyyyy...'
xs.insert(1, "hello")
list(xs) # → [42, 'hello', b'yyyyyy...']
xs.extend([1.5, b"z" * 200])
len(xs) # → 5
# Slice access resolves spilled values
xs[1:3] # → ['hello', b'yyyyyy...']
# Slice assignment with large values works
xs[2:4] = [99, b"w" * 200]
# contains / index / count
42 in xs # → True
xs.index(42) # → 0
xs.count(42) # → 1
# pop
val = xs.pop() # → b'zzzzzz...'
len(xs) # → 4
# reverse
xs.reverse()
# sort (including spilled values)
ys = SwapList(size_threshold=50)
ys.extend([b"c" * 20, b"a" * 20, b"b" * 20])
ys.sort()
list(ys) # → [b'aaaa...', b'bbbb...', b'cccc...']
# comparison works transparently
zs = SwapList([1, 2])
zs == [1, 2] # → True
zs == [1, 3] # → False
# concatenation / repetition
zs + [3, 4] # → [1, 2, 3, 4]
zs * 3 # → [1, 2, 1, 2, 1, 2]
xs.clear()
len(xs) # → 0
How It Works
SwapDict / SwapList automatically offloads values whose pickled size
meets or exceeds size_threshold to a SQLite database. Any pickle-able
object (not just bytes) is eligible.
| Value | Storage |
|---|---|
Pickle size < size_threshold |
In-memory |
Pickle size >= size_threshold |
SQLite (transparent) |
Spilled values are retrieved, updated, and deleted using the same dict/list
interface — no special API calls needed.
Caveats
size_thresholdis the pickle-size threshold in bytes; values >= this size are spilled.size_thresholdcannot be changed after instantiation.- Any pickle-able object type (not only
bytes) can be spilled. - Deleting the SQLite database will cause errors on previously spilled items.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swapcollection-1.0.1.tar.gz.
File metadata
- Download URL: swapcollection-1.0.1.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c5eb6b2b796b9fdf380d3ea57c7a0182aa4becaa2c075147f9483b9c84de1a5
|
|
| MD5 |
a9a8ca4e60de8fd05f5ce5ac0e08a8a9
|
|
| BLAKE2b-256 |
d75b50c5561ab4e0fe5b20908ff684905d973223d69d4dac1241c6f16b9e2391
|
File details
Details for the file swapcollection-1.0.1-py3-none-any.whl.
File metadata
- Download URL: swapcollection-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
298c54c2e29c7dc25f30a27816e97fa65ea7e5dda64eb7a67f908a46f0d7292f
|
|
| MD5 |
b9d31ee7ae04f92f0446d566ca92a59b
|
|
| BLAKE2b-256 |
a53110ee7589bccebefe3a07ecdf303f0ab86b77be5af686a394579e3b6a34f1
|