Skip to main content

A small package that binds dataclasses to an sqlite database

Project description

Datalite

Datalite is a simple Python package that binds your dataclasses to a table in a sqlite3 database, using it is extremely simple, say that you have a dataclass definition, just add the decorator @datalite(db_name="db.db") to the top of the definition, and the dataclass will now be bound to the file db.db

For example:

from dataclasses import dataclass
from datalite import datalite


@datalite(db_path="db.db")
@dataclass
class Student:
    student_id: int
    student_name: str = "John Smith"

This snippet will generate a table in the sqlite3 database file db.db with table name student and rows student_id, student_name with datatypes integer and text, respectively. The default value for student_name is John Smith.

Entry manipulation

After creating an object traditionally, given that you used the datalite decorator, the object has two new methods: .create_entry() and .remove_entry(), you can add the object to its associated table using the former, and remove it using the latter.

student = Student(10, "Albert Einstein")
student.create_entry()  # Adds the entry to the table associated in db.db
student.remove_entry()  # Removes from the table.

But what if you have created your object in a previous session, or wish to remove an object unreachable? ie: If the object is already garbage collected by the Python interpreter? remove_from(class_, obj_id) is a function that can be used for this express purpose, for instance:

remove_from(Student, 2)  # Removes the Student with obj_id 2.

Object IDs are auto-incremented, and correspond to the order the entry were inserted onto the system.

Fetching Records

:warning: Limitation! Fetch can only fetch limited classes correctly: int, float and str!

Finally, you may wish to recreate objects from a table that already exist, for this purpose we have the function fetch_from(class_, object_id) as well as is_fetchable(className, object_id) former fetches a record from the SQL database whereas the latter checks if it is fetchable (most likely to check if it exists.)

>>> fetch_from(Student, 2)
Student(student_id=10, student_name='Albert Einstein')

We have three helper methods, fetch_range(class_, range_) and fetch_all(class_) are very similar: the former fetches the records fetchable from the object id range provided by the user, whereas the latter fetches all records. Both return a tuple of class_ objects.

The last helper method, fetch_if(class_, condition) fetches all the records of type class_ that fit a certain condition. Here conditions must be written is SQL syntax.

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

datalite-0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

datalite-0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file datalite-0.2.tar.gz.

File metadata

  • Download URL: datalite-0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.1 CPython/3.8.2

File hashes

Hashes for datalite-0.2.tar.gz
Algorithm Hash digest
SHA256 b25c4d02364f03eb566f17c14233587564820b92e39d67b0fa8fea4a54cb6e82
MD5 aced65d5976cbd07968eaed046335bbc
BLAKE2b-256 c1a080f45a6f68f0c58c079b8516fb7de63cd7953659056e19117f8c0795141c

See more details on using hashes here.

File details

Details for the file datalite-0.2-py3-none-any.whl.

File metadata

  • Download URL: datalite-0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.1 CPython/3.8.2

File hashes

Hashes for datalite-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 85f17899c954aa84f5068f6a3479bd5eee1e09f2dd9546cf9028707351cb7dac
MD5 f6571477d9a34c6f5da7df1ea0c51244
BLAKE2b-256 9b8c2315f15ca7fa2791003079cb1f47267b19112c50479624e9c5ab708ac109

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