db_entity
This module defines the DatabaseEntity abstract base class, which serves as a common interface
for interacting with database entities such as studies, runs, and workers. The DatabaseEntity
class provides a standardized structure for managing these entities, including methods for
saving, deleting, reloading, and retrieving additional metadata.
Classes that inherit from DatabaseEntity must implement the abstract methods defined in the base
class, ensuring consistency across different types of database entities. This abstraction reduces
code duplication and promotes maintainability by centralizing shared functionality.
DatabaseEntity
Bases: Generic[T], ABC
Abstract base class for database entities such as runs, studies, and workers.
This class defines the common interface and behavior for interacting with database entities, including saving, deleting, and reloading data.
Attributes:
| Name | Type | Description |
|---|---|---|
entity_info |
T
|
The data model containing information about the entity. |
backend |
ResultsBackend
|
The backend instance used to interact with the database. |
Methods:
| Name | Description |
|---|---|
__repr__ |
Provide a string representation of the entity. |
__str__ |
Provide a human-readable string representation of the entity. |
reload_data |
Reload the latest data for this entity from the database. |
get_id |
Get the unique ID for this entity. |
get_additional_data |
Get any additional data saved to this entity. |
save |
Save the current state of this entity to the database. |
_post_save_hook |
Hook called after saving the entity to the database. Subclasses can override this method to add additional behavior. |
load |
Load an entity from the database by its ID. |
delete |
Delete an entity from the database by its ID. |
entity_type |
Get the type of this entity for database operations. |
_get_entity_type |
Get the entity type for database operations (used internally). |
Source code in merlin/db_scripts/entities/db_entity.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
entity_type
property
Get the type of this entity for database operations.
Returns:
| Type | Description |
|---|---|
str
|
The type name as a string. |
__init__(entity_info, backend)
Initialize a DatabaseEntity instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_info
|
T
|
The data model containing information about the entity. |
required |
backend
|
ResultsBackend
|
The backend instance used to interact with the database. |
required |
Source code in merlin/db_scripts/entities/db_entity.py
__repr__()
abstractmethod
Provide a string representation of the entity.
__str__()
abstractmethod
Provide a human-readable string representation of the entity.
delete(entity_identifier, backend)
classmethod
Delete an entity from the database by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_identifier
|
str
|
The ID of the entity to delete. |
required |
backend
|
ResultsBackend
|
A ResultsBackend instance. |
required |
Source code in merlin/db_scripts/entities/db_entity.py
get_additional_data()
Get any additional data saved to this entity.
Returns:
| Type | Description |
|---|---|
Dict
|
A dictionary of additional data. |
get_id()
Get the unique ID for this entity.
Returns:
| Type | Description |
|---|---|
str
|
The unique ID for this entity. |
load(entity_identifier, backend)
classmethod
Load an entity from the database by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_identifier
|
str
|
The ID of the entity to load. |
required |
backend
|
ResultsBackend
|
A |
required |
Returns:
| Type | Description |
|---|---|
E
|
An instance of the entity. |
Raises:
| Type | Description |
|---|---|
EntityNotFoundError
|
If an entry for the entity was not found in the database. |
Source code in merlin/db_scripts/entities/db_entity.py
reload_data()
Reload the latest data for this entity from the database.
Raises:
| Type | Description |
|---|---|
EntityNotFoundError
|
If an entry for this entity was not found in the database. |