API Reference

datalite Module

@datalite.datalite(db_path: str, type_overload: Optional[Dict[Optional[type], str]] = None) → Callable

Bind a dataclass to a sqlite3 database. This adds new methods to the class, such as create_entry(), remove_entry() and update_entry().

Parameters
  • db_path – Path of the database to be binded.

  • type_overload – Type overload dictionary.

Returns

The new dataclass.

datalite.constraints module

datalite.constraints module introduces constraint

types that can be used to hint field variables, that can be used to signal datalite decorator constraints in the database.

exception datalite.constraints.ConstraintFailedError

Bases: Exception

This exception is raised when a Constraint fails.

datalite.fetch module

datalite.fetch.fetch_all(class_: type, page: int = 0, element_count: int = 10) → tuple

Fetchall the records in the bound database.

Parameters
  • class – Class of the records.

  • page – Which page to retrieve, default all. (0 means closed).

  • element_count – Element count in each page.

Returns

All the records of type class_ in the bound database as a tuple.

datalite.fetch.fetch_equals(class_: type, field: str, value: Any) → Any

Fetch a class_ type variable from its bound db.

Parameters
  • class – Class to fetch.

  • field – Field to check for, by default, object id.

  • value – Value of the field to check for.

Returns

The object whose data is taken from the database.

datalite.fetch.fetch_from(class_: type, obj_id: int) → Any

Fetch a class_ type variable from its bound dv.

Parameters
  • class – Class to fetch from.

  • obj_id – Unique object id of the object.

Returns

The fetched object.

datalite.fetch.fetch_if(class_: type, condition: str, page: int = 0, element_count: int = 10) → tuple

Fetch all class_ type variables from the bound db, provided they fit the given condition

Parameters
  • class – Class type to fetch.

  • condition – Condition to check for.

  • page – Which page to retrieve, default all. (0 means closed).

  • element_count – Element count in each page.

Returns

A tuple of records that fit the given condition of given type class_.

datalite.fetch.fetch_range(class_: type, range_: range) → tuple

Fetch the records in a given range of object ids.

Parameters
  • class – Class of the records.

  • range – Range of the object ids.

Returns

A tuple of class_ type objects whose values come from the class_’ bound database.

datalite.fetch.fetch_where(class_: type, field: str, value: Any, page: int = 0, element_count: int = 10) → tuple

Fetch all class_ type variables from the bound db, provided that the field of the records fit the given value.

Parameters
  • class – Class of the records.

  • field – Field to check.

  • value – Value to check for.

  • page – Which page to retrieve, default all. (0 means closed).

  • element_count – Element count in each page.

Returns

A tuple of the records.

datalite.fetch.is_fetchable(class_: type, obj_id: int) → bool

Check if a record is fetchable given its obj_id and class_ type.

Parameters
  • class – Class type of the object.

  • obj_id – Unique obj_id of the object.

Returns

If the object is fetchable.

datalite.mass_actions module

This module includes functions to insert multiple records to a bound database at one time, with one time open and closing of the database file.

exception datalite.mass_actions.HeterogeneousCollectionError

Bases: Exception

:raiseif the passed collection is not homogeneous.

ie: If a List or Tuple has elements of multiple types.

datalite.mass_actions.copy_many(objects: Union[List[T], Tuple[T]], db_name: str, protect_memory: bool = True) → None

Copy many records to another database, from their original database to new database, do not delete old records.

Parameters
  • objects – Objects to copy.

  • db_name – Name of the new database.

  • protect_memory – Wheter to protect memory during operation, Setting this to False will quicken the operation, but if the operation is cut short, database file will corrupt.

Returns

None

datalite.mass_actions.create_many(objects: Union[List[T], Tuple[T]], protect_memory: bool = True) → None

Insert many records corresponding to objects in a tuple or a list.

Parameters
  • protect_memory – If False, memory protections are turned off, makes it faster.

  • objects – A tuple or a list of objects decorated with datalite.

Returns

None.

datalite.migrations module

Migrations module deals with migrating data when the object definitions change. This functions deal with Schema Migrations.

datalite.migrations.basic_migrate(class_: type, column_transfer: dict = None) → None

Given a class, compare its previous table, delete the fields that no longer exist, create new columns for new fields. If the column_flow parameter is given, migrate elements from previous column to the new ones. It should be noted that, the obj_ids do not persist.

Parameters
  • class – Datalite class to migrate.

  • column_transfer – A dictionary showing which columns will be copied to new ones.

Returns

None.