added merging

This commit is contained in:
Hellow 2023-03-03 12:32:08 +01:00
parent ebe3d9328c
commit 7502a38b58
2 changed files with 19 additions and 3 deletions

View File

@ -1,4 +1,4 @@
from typing import List from typing import List, Iterable
from .source import SourceAttribute from .source import SourceAttribute
from ..utils import string_processing from ..utils import string_processing
@ -60,7 +60,7 @@ class Collection:
if unified in self._by_attribute[name]: if unified in self._by_attribute[name]:
return self._by_attribute[name][unified] return self._by_attribute[name][unified]
def append(self, element: SourceAttribute, merge_on_conflict: bool = True): def append(self, element, merge_on_conflict: bool = True):
if type(element) is not self.element_type and self.element_type is not None: if type(element) is not self.element_type and self.element_type is not None:
raise TypeError(f"{type(element)} is not the set type {self.element_type}") raise TypeError(f"{type(element)} is not the set type {self.element_type}")
@ -76,6 +76,10 @@ class Collection:
self._data.append(element) self._data.append(element)
self.map_element(element) self.map_element(element)
def extend(self, element_list: Iterable, merge_on_conflict: bool = True):
for element in element_list:
self.append(element, merge_on_conflict=merge_on_conflict)
def __iter__(self): def __iter__(self):
for element in self._data: for element in self._data:
yield element yield element

View File

@ -25,7 +25,19 @@ class DatabaseObject:
self.dynamic = dynamic self.dynamic = dynamic
def merge(self, other, override: bool = False): def merge(self, other, override: bool = False):
for collection in if type(other) != type(self):
LOGGER.warning(f"can't merge \"{type(other)}\" into \"{type(self)}\"")
return
for collection in type(self).COLLECTION_ATTRIBUTES:
getattr(self, collection).extend(collection)
for simple_attribute in type(self).SIMPLE_ATTRIBUTES:
if getattr(other, simple_attribute) is None:
continue
if override or getattr(self, simple_attribute) is None:
setattr(self, simple_attribute, getattr(other, simple_attribute))
class MainObject(DatabaseObject): class MainObject(DatabaseObject):