From 7e92d5f244b8645052d9e99ea9195567b45f16c6 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 9 Mar 2023 22:35:31 +0100 Subject: [PATCH] Added recursive `.copy()` method to `Entity`. --- platypush/entities/_base.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/platypush/entities/_base.py b/platypush/entities/_base.py index a0ca8158c..fc792973f 100644 --- a/platypush/entities/_base.py +++ b/platypush/entities/_base.py @@ -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):