feat: implement proper merging for collections
This commit is contained in:
parent
208d6943b4
commit
92258e4dce
@ -107,7 +107,7 @@ print(only_smile)
|
|||||||
|
|
||||||
c = Collection([Song(title="hi"), Song(title="hi2"), Song(title="hi3")])
|
c = Collection([Song(title="hi"), Song(title="hi2"), Song(title="hi3")])
|
||||||
c1 = Collection([Song(title="he"), Song(title="hi5")])
|
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")])
|
c2 = Collection([Song(title="heeee")])
|
||||||
|
|
||||||
b = Collection([Song(title="some b"), Song(title="other b")])
|
b = Collection([Song(title="some b"), Song(title="other b")])
|
||||||
@ -130,9 +130,14 @@ print(c1.data)
|
|||||||
|
|
||||||
c11.append(Song(title="after creation"))
|
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()
|
||||||
print(c.data, len(c))
|
print(c.data, len(c))
|
||||||
print(c1.data)
|
print(c1.data)
|
||||||
|
print([obj.genre for obj in c.data])
|
||||||
|
|
||||||
print()
|
print()
|
||||||
print("c: ", c)
|
print("c: ", c)
|
||||||
|
@ -8,10 +8,6 @@ read_config()
|
|||||||
from . import cli
|
from . import cli
|
||||||
|
|
||||||
|
|
||||||
# I am SO sorry
|
|
||||||
print(sys.setrecursionlimit(500))
|
|
||||||
|
|
||||||
|
|
||||||
# configure logger default
|
# configure logger default
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging_settings['log_level'] if not DEBUG_LOGGIN else logging.DEBUG,
|
level=logging_settings['log_level'] if not DEBUG_LOGGIN else logging.DEBUG,
|
||||||
|
@ -25,7 +25,7 @@ class Collection(Generic[T]):
|
|||||||
|
|
||||||
self.extend(data)
|
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:
|
for name, value in __object.indexing_values:
|
||||||
if value is None:
|
if value is None:
|
||||||
continue
|
continue
|
||||||
@ -62,8 +62,7 @@ class Collection(Generic[T]):
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
for collection in self.contained_collections:
|
for collection in self.contained_collections:
|
||||||
if collection._contained_in_self(__object):
|
return collection._contained_in(__object)
|
||||||
return collection
|
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -80,13 +79,16 @@ class Collection(Generic[T]):
|
|||||||
if value is None:
|
if value is None:
|
||||||
continue
|
continue
|
||||||
if value in self._indexed_values[name]:
|
if value in self._indexed_values[name]:
|
||||||
existing_object = self._indexed_to_objects[value]
|
existing_object = self._indexed_to_objects[value][0]
|
||||||
break
|
break
|
||||||
|
|
||||||
if existing_object is None:
|
if existing_object is None:
|
||||||
return 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:
|
if existing_object is not __object:
|
||||||
raise ValueError("This should NEVER happen. Merging doesn't work.")
|
raise ValueError("This should NEVER happen. Merging doesn't work.")
|
||||||
|
@ -7,7 +7,6 @@ from .metadata import Metadata
|
|||||||
from .option import Options
|
from .option import Options
|
||||||
from ..utils.shared import HIGHEST_ID
|
from ..utils.shared import HIGHEST_ID
|
||||||
from ..utils.config import main_settings, logging_settings
|
from ..utils.config import main_settings, logging_settings
|
||||||
from ..utils.functions import replace_all_refs
|
|
||||||
|
|
||||||
|
|
||||||
LOGGER = logging_settings["object_logger"]
|
LOGGER = logging_settings["object_logger"]
|
||||||
@ -146,7 +145,7 @@ class DatabaseObject:
|
|||||||
|
|
||||||
return list()
|
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:
|
if other is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -169,9 +168,6 @@ class DatabaseObject:
|
|||||||
if override or getattr(self, simple_attribute) == default_value:
|
if override or getattr(self, simple_attribute) == default_value:
|
||||||
setattr(self, simple_attribute, getattr(other, simple_attribute))
|
setattr(self, simple_attribute, getattr(other, simple_attribute))
|
||||||
|
|
||||||
if replace_all_refs:
|
|
||||||
replace_all_refs(self, other)
|
|
||||||
|
|
||||||
def strip_details(self):
|
def strip_details(self):
|
||||||
for collection in type(self).DOWNWARDS_COLLECTION_STRING_ATTRIBUTES:
|
for collection in type(self).DOWNWARDS_COLLECTION_STRING_ATTRIBUTES:
|
||||||
getattr(self, collection).clear()
|
getattr(self, collection).clear()
|
||||||
|
Loading…
Reference in New Issue
Block a user