Skip to main content

Python collections that are backended by sqlite3 DB and are compatible with the built-in collections

Project description

sqlitecollections

sqlitecollections is a sort of containers that are backended by sqlite3 DB and are compatible with corresponding built-in collections. Since containers consume disk space instead of RAM, they can handle large amounts of data even in environments with limited RAM. Migrating from existing code using the built-in container is as simple as importing the library and changing the constructor.

The elements of the container are automatically serialized and stored in the sqlite3 database, and are automatically read from the sqlite3 database and deserialized when accessed. Current version supports List (mutable sequence), Dict (mutable mapping) and Set (mutable set) and almost all methods are compatible with list, dict and set respectively.

Installation

pip install sqlitecollections

Example

import sqlite3
from sqlitecollections import List, Set, Dict

conn = sqlite3.connect("collections.db")

l = List[str](
    connection=conn,
    table_name="list_example",
    data=["Alice", "Bob", "Carol"]
)
print(l[2])
#> Carol
print(len(l))
#> 3
l.append("Dave")
print(l.index("Bob"))
#> 2
d = Dict[str, str](
    connection=conn,
    table_name="dict_example",
    data={"a": "Alice", "b": "Bob"}
)
print(d["a"])
#> Alice
d["c"] = "Carol"
print(list(d.keys()))
#> ['a', 'b', 'c']
print(list(d.values()))
#> ['Alice', 'Bob', 'Carol']
s = Set[str](
    connection=conn,
    table_name="set_example",
    data=["Alice", "Bob", "Carol", "Dave"]
)
print("Ellen" in s)
#> False
print("Alice" in s)
#> True
print(list(s.intersection(["Alice", "Carol"])))
#> ['Alice', 'Carol']

The database is updated with each operation, so even if we exit from the python process at this point, the database will still be in the same state and the next time we use the same file, we will be able to use the container from the last time we terminated.

import sqlite3
from sqlitecollections import List

conn = sqlite3.connect("collections.db")

l = List[str](
    connection=conn,
    table_name="list_example",
)
print(len(l))
#> 4
print(l[2])
#> Carol

Pros and cons for built-in containers

Pros

  • Save memory usage.
  • Once the database is built, loading time is almost zero, even for huge data.

Cons

  • Each operation has the overhead of serialize/deserialize.
  • Some operations are incompatible and unavailable. For example, directly rewriting the mutable elements of a container.

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

sqlitecollections-0.5.5.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

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

sqlitecollections-0.5.5-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlitecollections-0.5.5.tar.gz.

File metadata

  • Download URL: sqlitecollections-0.5.5.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for sqlitecollections-0.5.5.tar.gz
Algorithm Hash digest
SHA256 95cd275641b83801bdb5ba421c3fbde4353d989c6fcc795239e8141ccb990c87
MD5 5c8252e32fd7feab5c1ad9b5b9bd6496
BLAKE2b-256 453739e8e06e30e1fa1fd7973cac18b285121b018286c4783a2d437223b87a81

See more details on using hashes here.

File details

Details for the file sqlitecollections-0.5.5-py3-none-any.whl.

File metadata

  • Download URL: sqlitecollections-0.5.5-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for sqlitecollections-0.5.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b0adffd3c40c52c5f07c1f59dc963a75ae0772eaacb321dd0cee2709a3e50bf5
MD5 ed7f8c4c7ba495774330e7f8bb55f331
BLAKE2b-256 a5cd89ba5dd4ab41898a8af6d17a6629d9e7330c9bd03c661e684fa107705381

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