feat: implement proper merging for collections

This commit is contained in:
Hellow 2023-10-24 13:32:26 +02:00
parent 208d6943b4
commit 92258e4dce
4 changed files with 14 additions and 15 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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.")

View File

@ -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()