API
- class SSD.core.database.Database(database_dir: str = '', database_name: str = 'database')[source]
- __init__(database_dir: str = '', database_name: str = 'database')[source]
Manage the creation and loading of Tables in the Database. User interface to dynamically add, get and update entries.
- Parameters:
database_dir – Directory which contains the Database file.
database_name – Name of the Database file.
- add_batch(table_name: str, batch: Dict[str, List[Any]])[source]
Execute a batch insert query. Return the indices of the new lines in the Table.
- Parameters:
table_name – Name of the Table.
batch – New lines of the Table.
- add_data(table_name: str, data: Dict[str, Any])[source]
Execute a line insert query. Return the index of the new line in the Table.
- Parameters:
table_name – Name of the Table.
data – New line of the Table.
- close(erase_file: bool = False)[source]
Close the Database.
- Parameters:
erase_file – If True, the Database file will be erased.
- create_fields(table_name: str, fields: Tuple[str, Type] | Tuple[str, Type, Any] | Tuple[str, str] | List[Tuple[str, Type] | Tuple[str, Type, Any] | Tuple[str, str]])[source]
Add new Fields to a Table.
- Parameters:
table_name – Name of the Table on which to add the new Fields.
fields – Name(s), type(s) and default value(s) of the Field(s) to add to the Table.
- create_table(table_name: str, storing_table: bool = True, fields: Tuple[str, Type] | Tuple[str, Type, Any] | Tuple[str, str] | List[Tuple[str, Type] | Tuple[str, Type, Any] | Tuple[str, str]] | None = None)[source]
Add a new Table to the Database with customizable Fields.
- Parameters:
table_name – Name of the Table to add to the Database.
storing_table – Specify whether the Table must be a storing or an exchange Table.
fields – Name(s), type(s) and default value(s) of the Field(s) to add to the Table.
- export(exporter: str, filename: str, tables: str | List[str] | None = None) None [source]
Export the Database to a CSV or JSON file.
- Parameters:
exporter – Exporter type (‘json’ or ‘csv’).
filename – Exported filename.
tables – Tables to export.
- get_fields(table_name: str, only_names: bool = True)[source]
Get the names of the Field(s) of a Tables of the Database.
- Parameters:
table_name – Name of the Table.
only_names – If False, returns a dict containing {‘table_name’: Table}.
- get_line(table_name: str, fields: str | List[str] | None = None, line_id: int = -1, joins: str | List[str] | None = None)[source]
Get a line of a Table.
- Parameters:
table_name – Name of the Table on which to perform the query.
fields – Name(s) of the Field(s) to request.
line_id – Index of the line to get.
joins – Name(s) of Table(s) to join to the selection.
- get_lines(table_name: str, fields: str | List[str] | None = None, lines_id: List[int] | None = None, lines_range: List[int] | None = None, joins: str | List[str] | None = None, batched: bool = False)[source]
Get a set of lines of a Table.
- Parameters:
table_name – Name of the Table on which to perform the query.
fields – Name(s) of the Field(s) to select.
lines_id – Indices of the lines to get. If not specified, ‘lines_range’ value will be used.
lines_range – Range of indices of the lines to get. If not specified, all lines will be selected.
joins – Name(s) of Table(s) to join to the selection.
batched – If True, data is returned as one batch per field. Otherwise, data is returned as list of lines.
- get_tables(only_names: bool = True)[source]
Get the names of created Tables in the Database.
- Parameters:
only_names – If True, only the names of the Tables will be returned in a List, otherwise the Tables themselves are returned in a Dict.
- load(show_architecture: bool = False) Database [source]
Load an existing Database file.
- Parameters:
show_architecture – If True, the loaded models will be printed.
- static make_name(table_name: str) str [source]
Harmonize the Table names.
- Parameters:
table_name – Name of the Table.
- property memory_size
Return the Database file memory size in bytes.
- nb_lines(table_name: str)[source]
Return the number of entries on a Table.
- Parameters:
table_name – Name of the Table.
- new(remove_existing: bool = False) Database [source]
Create a new Database file.
- Parameters:
remove_existing – If True, Database file will be overwritten.
- register_post_save_signal(table_name: str, handler: Callable, name: str | None = None)[source]
Connect a post_save signal from a Table to a handler.
- Parameters:
table_name – Name of the Table that will be sender.
handler – Executable code.
name – Name of the signal.
- register_pre_save_signal(table_name: str, handler: Callable, name: str | None = None)[source]
Connect a pre_save signal from a Table to a handler.
- Parameters:
table_name – Name of the Table that will be sender.
handler – Executable code.
name – Name of the signal.
- remove_field(table_name: str, field_name: str)[source]
Remove a Field of a Table of the Database.
- Parameters:
table_name – Name of the Table.
field_name – Current name of the Field to remove.
- remove_table(table_name: str)[source]
Remove a Table from the Database.
- Parameters:
table_name – Name of the Table.
- rename_field(table_name: str, field_name: str, new_field_name: str)[source]
Rename a Field of a Table of the Database.
- Parameters:
table_name – Name of the Table.
field_name – Current name of the Field to rename.
new_field_name – New name of the Field.
Utils
- SSD.core.utils.export(database_file: str, exporter: str, filename: str | None = None) None [source]
Export the Database file to CSV or JSON formats.
- Parameters:
database_file – Database filename.
exporter – Exporter type (either ‘csv’ or ‘json’).
filename – Exported filename.
- SSD.core.utils.merge(database_files: List[str], new_database_file: str = 'merged', remove_existing: bool = False)[source]
Merge Databases in a new Database.
- Parameters:
database_files – List of Databases files.
new_database_file – Name of the new Database.
remove_existing – If True, Database file with name ‘new_database_name’ will be overwritten.
- SSD.core.utils.remove_field(database_file: str, table_name: str, fields: str | List[str])[source]
Remove Fields of a Table of the Database.
- Parameters:
database_file – Database filename.
table_name – Name of the Table.
fields – Field(s) to remove from the Table.
- SSD.core.utils.remove_table(database_file: str, table_names: str | List[str])[source]
Remove Tables of the Database.
- Parameters:
database_file – Database filename.
table_names – Table(s) to remove from the Database.
- SSD.core.utils.rename_fields(database_file: str, table_name: str, renamed_fields: Tuple[str, str] | List[Tuple[str, str]])[source]
Rename Fields of a Table of the Database.
- Parameters:
database_file – Database filename.
table_name – Name of the Table.
renamed_fields – Tuple or list of tuples defined as (‘old_name’, ‘new_name’).