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 untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -115,6 +115,17 @@ if 'entity' not in Base.metadata:
""" """
return str(self.external_id), str(self.plugin) 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: def _serialize_value(self, col: ColumnProperty) -> Any:
val = getattr(self, col.key) val = getattr(self, col.key)
if isinstance(val, datetime): if isinstance(val, datetime):