added merging
This commit is contained in:
parent
ebe3d9328c
commit
7502a38b58
@ -1,4 +1,4 @@
|
||||
from typing import List
|
||||
from typing import List, Iterable
|
||||
|
||||
from .source import SourceAttribute
|
||||
from ..utils import string_processing
|
||||
@ -60,7 +60,7 @@ class Collection:
|
||||
if unified in self._by_attribute[name]:
|
||||
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:
|
||||
raise TypeError(f"{type(element)} is not the set type {self.element_type}")
|
||||
|
||||
@ -76,6 +76,10 @@ class Collection:
|
||||
self._data.append(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):
|
||||
for element in self._data:
|
||||
yield element
|
||||
|
@ -25,7 +25,19 @@ class DatabaseObject:
|
||||
self.dynamic = dynamic
|
||||
|
||||
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):
|
||||
|
Loading…
Reference in New Issue
Block a user