fix/reindex_before_collection #21

Merged
Hazel merged 32 commits from fix/reindex_before_collection into experimental 2024-05-06 17:36:28 +00:00
7 changed files with 31 additions and 39 deletions
Showing only changes of commit 3e29e1d322 - Show all commits

View File

@ -6,8 +6,8 @@ logging.getLogger().setLevel(logging.DEBUG)
if __name__ == "__main__":
commands = [
"s: #a Ghost Bath",
"d: 14",
"s: #a Crystal F",
"d: 20",
]

View File

@ -53,9 +53,9 @@ class Artwork:
def get_variant_name(self, variant: ArtworkVariant) -> str:
return f"artwork_{variant['width']}x{variant['height']}_{hash_url(variant['url']).replace('/', '_')}"
def __merge__(self, other: Artwork, override: bool = False) -> None:
def __merge__(self, other: Artwork, **kwargs) -> None:
for key, value in other._variant_mapping.items():
if key not in self._variant_mapping or override:
if key not in self._variant_mapping:
self._variant_mapping[key] = value
def __eq__(self, other: Artwork) -> bool:

View File

@ -79,26 +79,25 @@ class Collection(Generic[T]):
self._map_element(e)
def _find_object(self, __object: T) -> Optional[T]:
self._remap()
for name, value in __object.indexing_values:
if value in self._indexed_values[name]:
return self._indexed_values[name][value]
def append(self, __object: Optional[T], already_is_parent: bool = False, from_map: bool = False):
def append(self, __object: Optional[T], **kwargs):
"""
If an object, that represents the same entity exists in a relevant collection,
merge into this object. (and remap)
Else append to this collection.
:param __object:
:param already_is_parent:
:param from_map:
:return:
"""
if __object is None:
return
self._remap()
existing_object = self._find_object(__object)
if existing_object is None:
@ -107,40 +106,39 @@ class Collection(Generic[T]):
self._map_element(__object)
for collection_attribute, child_collection in self.extend_object_to_attribute.items():
__object.__getattribute__(collection_attribute).extend(child_collection)
__object.__getattribute__(collection_attribute).extend(child_collection, **kwargs)
for attribute, new_object in self.append_object_to_attribute.items():
__object.__getattribute__(attribute).append(new_object)
__object.__getattribute__(attribute).append(new_object, **kwargs)
# only modify collections if the object actually has been appended
for attribute, a in self.sync_on_append.items():
b = __object.__getattribute__(attribute)
object_trace(f"Syncing [{a}{id(a)}] = [{b}{id(b)}]")
if a is b:
continue
object_trace(f"Syncing [{a}] = [{b}]")
a._collection_for.update(b._collection_for)
for synced_with, key in b._collection_for.items():
synced_with.__setattr__(key, a)
a._collection_for.update(b._collection_for)
a.extend(b.data, **kwargs)
else:
# merge only if the two objects are not the same
if existing_object.id == __object.id:
return
old_id = existing_object.id
existing_object.merge(__object)
if existing_object.id != old_id:
self._unmap_element(old_id)
existing_object.merge(__object, **kwargs)
self._map_element(existing_object)
def extend(self, __iterable: Optional[Generator[T, None, None]]):
def extend(self, __iterable: Optional[Generator[T, None, None]], **kwargs):
if __iterable is None:
return
for __object in __iterable:
self.append(__object)
self.append(__object, **kwargs)
@property
def data(self) -> List[T]:
@ -156,8 +154,8 @@ class Collection(Generic[T]):
def __iter__(self) -> Iterator[T]:
yield from self._data
def __merge__(self, __other: Collection, override: bool = False):
self.extend(__other)
def __merge__(self, __other: Collection, **kwargs):
self.extend(__other, **kwargs)
def __getitem__(self, item: int):
return self._data[item]

View File

@ -50,10 +50,9 @@ class InnerData:
def __hash__(self):
return self.id
def __merge__(self, __other: InnerData, override: bool = False):
def __merge__(self, __other: InnerData, **kwargs):
"""
:param __other:
:param override:
:return:
"""
@ -68,13 +67,9 @@ class InnerData:
# if the object of value implemented __merge__, it merges
existing = self.__getattribute__(key)
if hasattr(type(existing), "__merge__"):
existing.__merge__(value, override)
existing.__merge__(value, **kwargs)
continue
# override the existing value if requested
if override:
self.__setattr__(key, value)
class OuterProxy:
"""
@ -174,13 +169,12 @@ class OuterProxy:
def __eq__(self, other: Any):
return self.__hash__() == other.__hash__()
def merge(self, __other: Optional[OuterProxy], override: bool = False):
def merge(self, __other: Optional[OuterProxy], **kwargs):
"""
1. merges the data of __other in self
2. replaces the data of __other with the data of self
:param __other:
:param override:
:return:
"""
if __other is None:
@ -205,11 +199,11 @@ class OuterProxy:
instance._inner = a._inner
a._inner._refers_to_instances.add(instance)
a._inner.__merge__(old_inner, override=override)
a._inner.__merge__(old_inner, **kwargs)
del old_inner
def __merge__(self, __other: Optional[OuterProxy], override: bool = False):
self.merge(__other, override)
def __merge__(self, __other: Optional[OuterProxy], **kwargs):
self.merge(__other, **kwargs)
def mark_as_fetched(self, *url_hash_list: List[str]):
for url_hash in url_hash_list:

View File

@ -44,7 +44,7 @@ def get_collection_string(
ignore_titles: Set[str] = None,
background: BColors = OPTION_BACKGROUND,
foreground: BColors = OPTION_FOREGROUND,
add_id: bool = False,
add_id: bool = True,
) -> str:
if collection.empty:
return ""

View File

@ -91,7 +91,7 @@ class Source:
def __repr__(self) -> str:
return f"Src({self.page_enum.value}: {shorten_display_url(self.url)})"
def __merge__(self, other: Source, override: bool = False):
def __merge__(self, other: Source, **kwargs):
if self.audio_url is None:
self.audio_url = other.audio_url
self.additional_data.update(other.additional_data)
@ -150,7 +150,7 @@ class SourceCollection:
def __iter__(self):
yield from self.get_sources()
def __merge__(self, other: SourceCollection, override: bool = False):
def __merge__(self, other: SourceCollection, **kwargs):
self.extend(other)
@property

View File

@ -15,7 +15,7 @@ __stage__ = os.getenv("STAGE", "prod")
DEBUG = (__stage__ == "dev") and True
DEBUG_LOGGING = DEBUG and False
DEBUG_TRACE = DEBUG and True
DEBUG_OBJECT_TRACE = DEBUG and False
DEBUG_OBJECT_TRACE = DEBUG and True
DEBUG_OBJECT_TRACE_CALLSTACK = DEBUG_OBJECT_TRACE and False
DEBUG_YOUTUBE_INITIALIZING = DEBUG and False
DEBUG_PAGES = DEBUG and False