feat: cleaned the modification of the new collections

This commit is contained in:
Hazel 2024-04-17 13:39:58 +02:00
parent 0b216b7d40
commit 42c4a04b62
1 changed files with 11 additions and 17 deletions

View File

@ -58,16 +58,6 @@ class Collection(Generic[T]):
self._indexed_values[name].add(value)
self._indexed_to_objects[value].append(__object)
if not from_map:
for attribute, new_object in self.contain_given_in_attribute.items():
__object.__getattribute__(attribute).contain_collection_inside(new_object)
for attribute, new_object in self.contain_attribute_in_given.items():
new_object.contain_collection_inside(__object.__getattribute__(attribute))
for attribute, new_object in self.append_object_to_attribute.items():
__object.__getattribute__(attribute).append(new_object)
def _unmap_element(self, __object: T):
if __object.id in self._contains_ids:
self._contains_ids.remove(__object.id)
@ -186,11 +176,6 @@ class Collection(Generic[T]):
def contains(self, __object: T) -> bool:
return len(self._contained_in_sub(__object)) > 0
def _append(self, __object: T, from_map: bool = False):
print(self, __object)
self._map_element(__object, from_map=from_map)
self._data.append(__object)
def _find_object_in_self(self, __object: T) -> Optional[T]:
for name, value in __object.indexing_values:
if value is None or value == __object._default_factories.get(name, lambda: None)():
@ -211,6 +196,7 @@ class Collection(Generic[T]):
if no_sibling:
return self, None
"""
# find in siblings and all children of siblings
for parent in self.parents:
for sibling in parent.children:
@ -220,6 +206,7 @@ class Collection(Generic[T]):
o, other_object = sibling._find_object(__object, no_sibling=True)
if other_object is not None:
return o, other_object
"""
return self, None
@ -243,12 +230,19 @@ class Collection(Generic[T]):
if existing_object is None:
# append
append_to._data.append(__object)
append_to._map_element(__object, from_map=from_map)
append_to._map_element(__object)
# only modify collections if the object actually has been appended
for collection_attribute, new_object in self.contain_given_in_attribute.items():
__object.__getattribute__(collection_attribute).contain_collection_inside(new_object)
for attribute, new_object in self.append_object_to_attribute.items():
__object.__getattribute__(attribute).append(new_object)
else:
# merge
append_to._unmap_element(existing_object)
existing_object.merge(__object)
append_to._map_element(existing_object, from_map=from_map)
append_to._map_element(existing_object)
def extend(self, __iterable: Optional[Iterable[T]], from_map: bool = False):