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 sqlitecollections as sc

l = sc.List[str](["Alice", "Bob", "Carol"])
print(l[2])
#> Carol
print(len(l))
#> 3
l.append("Dave")
print(l.index("Bob"))
#> 1
print(l.index("Dave"))
#> 3

d = sc.Dict[str, str]({"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 = sc.Set[str](["Alice", "Bob", "Carol", "Dave"])
print("Ellen" in s)
#> False
print("Alice" in s)
#> True
print(list(s.intersection(["Alice", "Carol"])))
#> ['Alice', 'Carol']

In the above example, a temporary file is created every time a container is created, and the elements are written to the sqlite3 database created on the file, thus consuming very little RAM.

If you want to reuse the container you created, you can create it by specifying the file path and table name of the sqlite3 database.

import sqlitecollections as sc

l = sc.List[str](["Alice", "Bob", "Carol"], connection="path/to/file.db", table_name="list_example")
l.append("Dave")
exit()

When you load it, you can restore the previous state by specifying the same file path and table name.

import sqlitecollections as sc

l = sc.List[str](connection="path/to/file.db", table_name="list_example")
print(len(l))
#> 4
print(list(l))
#> ['Alice', 'Bob', 'Carol', 'Dave']

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.9.5.tar.gz (16.2 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.9.5-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlitecollections-0.9.5.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for sqlitecollections-0.9.5.tar.gz
Algorithm Hash digest
SHA256 9e6f9344331510b1f2a5ea4fcba860c33f12cb714699146ac4d95d70fa96c48f
MD5 9100dcde5131be00304a1a799910e5bd
BLAKE2b-256 2f2ec913ad1693b97fca6c1503ac462092976bb64dcd22bd09cfd9df93b355a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlitecollections-0.9.5-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for sqlitecollections-0.9.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a61457939cd3907daaf2306dcf1e624584d7d0fd63dc23ed3be9da5700a3e864
MD5 e85b2386d1366b267cb1fc08be0bd7c8
BLAKE2b-256 5459d5cb0dd72578a8b3ee212cc2f939daa8ffa7bee2ed6978dc96243c43eb37

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