From 1d3cef4ab8f35a6938e45ab905c81c77d8a882c3 Mon Sep 17 00:00:00 2001 From: Paul Hewlett Date: Wed, 12 May 2021 15:16:50 +0100 Subject: [PATCH] Cache subclass instances Problem: On instantiation of archivist extra subclassesa are also instantiated even if not needed. Solution: Hide instantiation of subclasses behind a property. Signed-off-by: Paul Hewlett --- archivist/archivist.py | 36 ++++++++++++++++++++++++++++++++---- unittests/testarchivist.py | 24 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/archivist/archivist.py b/archivist/archivist.py index 7bf7f686..86804c65 100644 --- a/archivist/archivist.py +++ b/archivist/archivist.py @@ -67,10 +67,38 @@ def __init__(self, url, *, auth=None, cert=None, verify=True): self._cert = cert - self.assets = _AssetsClient(self) - self.events = _EventsClient(self) - self.locations = _LocationsClient(self) - self.attachments = _AttachmentsClient(self) + self._assets = None + self._events = None + self._locations = None + self._attachments = None + + @property + def assets(self): + """docstring""" + if self._assets is None: + self._assets = _AssetsClient(self) + return self._assets + + @property + def events(self): + """docstring""" + if self._events is None: + self._events = _EventsClient(self) + return self._events + + @property + def locations(self): + """docstring""" + if self._locations is None: + self._locations = _LocationsClient(self) + return self._locations + + @property + def attachments(self): + """docstring""" + if self._attachments is None: + self._attachments = _AttachmentsClient(self) + return self._attachments @property def headers(self): diff --git a/unittests/testarchivist.py b/unittests/testarchivist.py index 159cfc06..f8f2ef08 100644 --- a/unittests/testarchivist.py +++ b/unittests/testarchivist.py @@ -33,6 +33,30 @@ def test_archivist(self): Test default archivist creation """ arch = Archivist("url", auth="authauthauth") + self.assertIsNotNone( + arch.assets, + msg="Incorrect assets", + ) + self.assertIsNotNone( + arch.events, + msg="Incorrect events", + ) + self.assertIsNotNone( + arch.locations, + msg="Incorrect locations", + ) + self.assertIsNotNone( + arch.locations, + msg="Incorrect locations", + ) + self.assertIsNotNone( + arch.attachments, + msg="Incorrect attachments", + ) + self.assertIsNotNone( + arch.attachments, + msg="Incorrect attachments", + ) self.assertEqual( arch.url, "url",