From 92258e4dce625707a73b7202e02d9f037fdc5eb8 Mon Sep 17 00:00:00 2001 From: Hellow <74311245+HeIIow2@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:32:26 +0200 Subject: [PATCH] feat: implement proper merging for collections --- src/create_custom_objects.py | 7 ++++++- src/music_kraken/__init__.py | 4 ---- src/music_kraken/objects/new_collection.py | 12 +++++++----- src/music_kraken/objects/parents.py | 6 +----- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/create_custom_objects.py b/src/create_custom_objects.py index e4cb12f..5eddb2d 100644 --- a/src/create_custom_objects.py +++ b/src/create_custom_objects.py @@ -107,7 +107,7 @@ print(only_smile) c = Collection([Song(title="hi"), Song(title="hi2"), Song(title="hi3")]) c1 = Collection([Song(title="he"), Song(title="hi5")]) -c11 = Collection([Song(title="wow how ultra subby")]) +c11 = Collection([Song(title="wow how ultra subby", isrc="hiii")]) c2 = Collection([Song(title="heeee")]) b = Collection([Song(title="some b"), Song(title="other b")]) @@ -130,9 +130,14 @@ print(c1.data) c11.append(Song(title="after creation")) +other_song = Song(title="has same isrc", isrc="hiii", genre="hssss") +print(c.contains(other_song)) +c.append(other_song) + print() print(c.data, len(c)) print(c1.data) +print([obj.genre for obj in c.data]) print() print("c: ", c) diff --git a/src/music_kraken/__init__.py b/src/music_kraken/__init__.py index 555b154..a803b48 100644 --- a/src/music_kraken/__init__.py +++ b/src/music_kraken/__init__.py @@ -8,10 +8,6 @@ read_config() from . import cli -# I am SO sorry -print(sys.setrecursionlimit(500)) - - # configure logger default logging.basicConfig( level=logging_settings['log_level'] if not DEBUG_LOGGIN else logging.DEBUG, diff --git a/src/music_kraken/objects/new_collection.py b/src/music_kraken/objects/new_collection.py index 5569855..3b2565a 100644 --- a/src/music_kraken/objects/new_collection.py +++ b/src/music_kraken/objects/new_collection.py @@ -25,7 +25,7 @@ class Collection(Generic[T]): self.extend(data) - def _map_element(self, __object: T, no_append: bool = True): + def _map_element(self, __object: T, no_append: bool = False): for name, value in __object.indexing_values: if value is None: continue @@ -62,8 +62,7 @@ class Collection(Generic[T]): return self for collection in self.contained_collections: - if collection._contained_in_self(__object): - return collection + return collection._contained_in(__object) return None @@ -80,13 +79,16 @@ class Collection(Generic[T]): if value is None: continue if value in self._indexed_values[name]: - existing_object = self._indexed_to_objects[value] + existing_object = self._indexed_to_objects[value][0] break if existing_object is None: return None - existing_object.merge(__object, replace_all_refs=True) + existing_object.merge(__object) + replace_all_refs(existing_object, __object) + + print(existing_object, __object) if existing_object is not __object: raise ValueError("This should NEVER happen. Merging doesn't work.") diff --git a/src/music_kraken/objects/parents.py b/src/music_kraken/objects/parents.py index 808cff1..c3d8b1e 100644 --- a/src/music_kraken/objects/parents.py +++ b/src/music_kraken/objects/parents.py @@ -7,7 +7,6 @@ from .metadata import Metadata from .option import Options from ..utils.shared import HIGHEST_ID from ..utils.config import main_settings, logging_settings -from ..utils.functions import replace_all_refs LOGGER = logging_settings["object_logger"] @@ -146,7 +145,7 @@ class DatabaseObject: return list() - def merge(self, other, override: bool = False, replace_all_refs: bool = False): + def merge(self, other, override: bool = False): if other is None: return @@ -169,9 +168,6 @@ class DatabaseObject: if override or getattr(self, simple_attribute) == default_value: setattr(self, simple_attribute, getattr(other, simple_attribute)) - if replace_all_refs: - replace_all_refs(self, other) - def strip_details(self): for collection in type(self).DOWNWARDS_COLLECTION_STRING_ATTRIBUTES: getattr(self, collection).clear()