From f471c6a72be032973bac35040384f9f9d1934a6e Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Fri, 16 Jun 2023 10:43:35 +0200 Subject: [PATCH] implemented the deletion of unnecesarry details in not chosen objects, for the sake of memory efficiency in the history --- src/music_kraken/cli/download/shell.py | 5 ++++- src/music_kraken/download/results.py | 7 +++++++ src/music_kraken/objects/collection.py | 3 +++ src/music_kraken/objects/parents.py | 4 ++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/music_kraken/cli/download/shell.py b/src/music_kraken/cli/download/shell.py index 91f546f..03e804f 100644 --- a/src/music_kraken/cli/download/shell.py +++ b/src/music_kraken/cli/download/shell.py @@ -167,7 +167,7 @@ class Shell: self.max_displayed_options = max_displayed_options self.option_digits: int = option_digits - self.current_results: Results = SearchResults + self.current_results: Results = None self._result_history: List[Results] = [] self.genre = genre or get_genre() @@ -296,6 +296,9 @@ class Shell: page: Type[Page] music_object: DatabaseObject + if self.current_results is not None: + self.current_results.delete_details(index) + try: page, music_object = self.current_results.get_music_object_by_index(index) except KeyError: diff --git a/src/music_kraken/download/results.py b/src/music_kraken/download/results.py index fa41369..631ad48 100644 --- a/src/music_kraken/download/results.py +++ b/src/music_kraken/download/results.py @@ -29,6 +29,13 @@ class Results: def get_music_object_by_index(self, index: int) -> Tuple[Type[Page], DatabaseObject]: # if this throws a key error, either the formated generator needs to be iterated, or the option doesn't exist. return self._page_by_index[index], self._by_index[index] + + def delete_details(self, exclude_index: int): + for index, music_object in self._by_index.items(): + if index == exclude_index: + continue + + music_object.strip_details() class SearchResults(Results): diff --git a/src/music_kraken/objects/collection.py b/src/music_kraken/objects/collection.py index 2daf0fa..675fd7e 100644 --- a/src/music_kraken/objects/collection.py +++ b/src/music_kraken/objects/collection.py @@ -158,3 +158,6 @@ class Collection: @property def empty(self) -> bool: return len(self._data) == 0 + + def clear(self): + self.__init__(element_type=self.element_type) diff --git a/src/music_kraken/objects/parents.py b/src/music_kraken/objects/parents.py index 9185a82..3121d4f 100644 --- a/src/music_kraken/objects/parents.py +++ b/src/music_kraken/objects/parents.py @@ -95,6 +95,10 @@ class DatabaseObject: if override or getattr(self, simple_attribute) == default_value: setattr(self, simple_attribute, getattr(other, simple_attribute)) + def strip_details(self): + for collection in type(self).DOWNWARDS_COLLECTION_ATTRIBUTES: + getattr(self, collection).clear() + @property def metadata(self) -> Metadata: return Metadata()