Added recursive `.copy()` method to `Entity`.

This commit is contained in:
Fabio Manganiello 2023-03-09 22:35:31 +01:00
parent 9f9ee575f1
commit 7e92d5f244
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
1 changed files with 11 additions and 0 deletions

View File

@ -115,6 +115,17 @@ if 'entity' not in Base.metadata:
"""
return str(self.external_id), str(self.plugin)
def copy(self) -> 'Entity':
"""
This method returns a copy of the entity. It's useful when you want
to reuse entity objects in other threads or outside of their
associated SQLAlchemy session context.
"""
return self.__class__(
**{col.key: getattr(self, col.key, None) for col in self.columns},
children=[child.copy() for child in self.children],
)
def _serialize_value(self, col: ColumnProperty) -> Any:
val = getattr(self, col.key)
if isinstance(val, datetime):