pydetecdiv.persistence.repository

Definition of the Repository interface accessible from the Business-logic layer and that must be implemented by concrete repositories.

class pydetecdiv.persistence.repository.ShallowDb

Bases: ABC

Abstract class used as an interface to encapsulate project persistence access

abstractmethod create() None

Abstract method enforcing the implementation in all shallow persistence connectors of a create() method for creating the database if it does not exist

abstractmethod commit() None

Abstract method enforcing the implementation of a method to save creations and updates of objects in repository on disk

abstractmethod rollback() None

Abstract method enforcing the implementation of a method cancelling operations that have not been committed

abstractmethod close() None

Abstract method enforcing the implementation of a close() method in all shallow persistence connectors

abstractmethod import_images(image_files: list[str], data_dir_path: str, destination: str, author: str = '', date: datetime = 'now', in_place: bool = False, img_format: str = 'imagetiff') Popen

Import images specified in a list of files into a destination

Parameters:
  • image_files – list of image files to import

  • data_dir_path – path for the current project raw data directory

  • destination – destination directory to import files into

  • author – the user importing the data

  • date – the date of import

  • in_place – boolean indicating whether image files should be copied (False) or kept in place (True)

  • img_format – the file format

Returns:

the list of imported files. This list can be used to roll the copy back if needed

abstractmethod save_object(class_name: str, record: dict[str, Any]) int

Abstract method enforcing the implementation of a save_object() method in all shallow persistence connectors

Parameters:
  • class_name – the class name of the object to save

  • record – the record representing the object

abstractmethod delete_object(class_name: str, id_: int) None

Abstract method enforcing the implementation of a delete_object() method in all shallow persistence connectors

Parameters:
  • class_name – the class name of the object to delete

  • id_ – the id of the object to delete

abstractmethod count_records(class_name: str) int

Get the number of objects of a given class in the current project

Parameters:

class_name – the class name of the objects whose count will be returned

Returns:

the number of objects

abstractmethod get_dataframe(class_name: str, id_list: list[int] = None) DataFrame

Abstract method enforcing the implementation of a method returning a pandas DataFrame containing the list of objects of a given class in the current project whose id is in id_list

Parameters:
  • class_name – the class name of the objects whose list will be returned

  • id_list – the list of ids of objects to retrieve

Returns:

a DataFrame containing the list of objects

abstractmethod get_record(class_name: str, id_: int = None, uuid: str = None) dict[str, Any] | None

Abstract method enforcing the implementation of a method returning an object record of a given class whose id is id_

Parameters:
  • class_name – the class of the requested object

  • id_ – the id of the requested object

Returns:

a record (i.e. dictionary) containing the data for the requested object

abstractmethod get_records(class_name: str, id_list: list[int] = None) list[dict[str, Any]]

Abstract method enforcing the implementation of a method returning the list of all object records of a given class specified by its name or a list of objects whose id is in id_list

Parameters:
  • class_name – the class name of the requested objects

  • id_list – the list of ids for objects

Returns:

a list of records (i.e. dictionaries) containing the data for the requested objects

abstractmethod get_linked_records(cls_name: str, parent_cls_name: str, parent_id: int) list[dict[str, Any]]
Abstract method enforcing the implementation of a method returning the list of records for all objects of class

defined by class_name that are linked to object of class parent_class_name with id_ = parent_id

Parameters:
  • cls_name – the class name of the objects to retrieve records for

  • parent_cls_name – the class nae of the parent object

  • parent_id – the id of the parent object

Returns:

a list of records

Create a link between two domain-specific objects. There must be a direct link defined in Linker class, otherwise, the link cannot be created.

Parameters:
  • class1_name – the class name of the first object to link

  • id_1 – the id of the first object to link

  • class2_name – the class name of the second object to link

  • id_2 – the id of the second object to link

Remove the link between two domain-specific objects. There must be a direct link defined in Linker class, otherwise, the link cannot be removed.

Parameters:
  • class1_name – the class name of the first object to unlink

  • id_1 – the id of the first object to unlink

  • class2_name – the class name of the second object to unlink

  • id_2 – the id of the second object to unlink

pydetecdiv.persistence.sqlalchemy

Concrete Repositories using a SQL database with the sqlalchemy toolkit

class pydetecdiv.persistence.sqlalchemy.repositories.ShallowSQLite3(dbname: str = None)

Bases: ShallowDb

A concrete shallow SQLite3 persistence inheriting ShallowDb and implementing SQLite3-specific engine

property session: Session

Property returning the sqlalchemy Session object attached to the repository

Returns:

commit() None

Commit the current transaction

rollback() None

Rollback the current transaction

executescript(script)

Reads a string containing several SQL statements in a free format

Parameters:

script (str) – the string representing the SQL script to be executed

create() None

Gets SqlAlchemy classes defining the project database schema and creates the database if it does not exist.

close() None

Close the current connexion

import_images(image_files: list[str], data_dir_path: str, destination: str, author: str = '', date: datetime = 'now', in_place: bool = False, img_format: str = 'imagetiff') Popen

Import images specified in a list of files into a destination

Parameters:
  • image_files (list of str) – list of image files to import

  • data_dir_path (path or str) – path for the current project raw data directory

  • destination (str) – destination directory to import files into

  • author (str) – the user importing the data

  • date (str) – the date of import

  • in_place (bool) – boolean indicating whether image files should be copied (False) or kept in place (True)

  • img_format (str) – the file format

Returns:

the list of imported files. This list can be used to roll the copy back if needed

Return type:

list of str

annotate_data(dataset: <module 'pydetecdiv.domain.Dataset' from '/home/fred/PycharmProjects/pyDetecDiv/src/pydetecdiv/domain/Dataset.py'>, source: str, keys_: tuple[str, ...], regex: str) DataFrame

Method to annotate data files in a dataset according to a regular expression applied to a source. The resulting key-value pairs are placed in a key_val column.

Parameters:
  • dataset (Dataset object) – the dataset whose data should be annotated

  • source (str or callable returning a str) – the database field or combination of fields to apply the regular expression to

  • keys (tuple of str) – the list of classes created objects belong to

  • regex (regular expression str) – regular expression defining the annotations

Returns:

list of annotated Data records in a dataframe

Return type:

pandas.DataFrame

save_object(class_name: str, record: dict[str, object]) int

Save the object represented by the record

Parameters:
  • class_name (str) – the class name of the object to save into SQL database

  • record (dict) – the record representing the object

Returns:

the id of the created or updated object

Return type:

int

delete_object(class_name: str, id_: int) None

Delete an object of class name = class_name with id = id_

Parameters:
  • class_name – the class name of the object to delete

  • id_ – the id of the object to delete

count_records(class_name: str) int

Get the number of objects of a given class in the current project

Parameters:

class_name (str) – the class name of the objects whose count will be returned

Returns:

the number of objects

Return type:

int

get_dataframe(class_name: str, id_list: list[int] = None) DataFrame

Get a DataFrame containing the list of all domain objects of a given class in the current project

Parameters:
  • class_name (str) – the class name of the objects whose list will be returned

  • id_list (a list of int) – the list of ids of objects to retrieve

Returns:

a DataFrame containing the list of objects

Return type:

DataFrame containing the records representing the requested domain-specific objects

get_record(class_name: str, id_: int = None, uuid: str = None) dict[str, Any] | None

A method returning an object record of a given class from its id

Parameters:
  • class_name (str) – the class name of object to get the record of

  • id (int) – the id of the requested object

Returns:

the object record

Return type:

dict (record)

get_record_by_name(class_name: str, name: str = None) dict[str, Any] | None

Return a record from its name

Parameters:
  • class_name (str) – class name of the corresponding DSO object

  • name (str) – the name of the requested record

Returns:

the record

Return type:

dict

get_records(class_name: str, id_list: list[int] = None) list[dict[str, Any]]

A method returning the list of all object records of a given class or select those whose id is in id_list

Parameters:
  • class_name (str) – the class name of objects to get records of

  • id_list (a list of int) – the list of ids of objects to retrieve

Returns:

a list of records

Return type:

list of dictionaries (records)

get_linked_records(cls_name: str, parent_cls_name: str, parent_id: int) list[dict[str, Any]]

A method returning the list of records for all objects of class defined by cls_name that are linked to object of class parent_cls_name with id_ = parent_id

Parameters:
  • cls_name – the class name of the objects to retrieve records for

  • parent_cls_name – the class nae of the parent object

  • parent_id – the id of the parent object

Returns:

a list of records

Create a link between two domain-specific objects. There must be a direct link defined in Linker class, otherwise, the link cannot be created.

Parameters:
  • class1_name (str) – the class name of the first object to link

  • id_1 (int) – the id of the first object to link

  • class2_name (str) – the class name of the second object to link

  • id_2 (int) – the id of the second object to link

Remove the link between two domain-specific objects. There must be a direct link defined in Linker class, otherwise, the link cannot be removed.

Parameters:
  • class1_name (str) – the class name of the first object to unlink

  • id_1 (int) – the id of the first object to unlink

  • class2_name (str) – the class name of the second object to unlink

  • id_2 (int) – the id of the second object to unlink