From 38e7620fbdc9782d5502679def99e1347610f32a Mon Sep 17 00:00:00 2001 From: Hellow Date: Thu, 9 Mar 2023 20:52:15 +0100 Subject: [PATCH] implemented DatabaseObject.indexing_values for each data objects --- src/music_kraken/objects/collection.py | 8 +++----- src/music_kraken/objects/parents.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/music_kraken/objects/collection.py b/src/music_kraken/objects/collection.py index 2153c73..846499f 100644 --- a/src/music_kraken/objects/collection.py +++ b/src/music_kraken/objects/collection.py @@ -1,17 +1,15 @@ -from typing import List, Iterable, Dict, DefaultDict +from typing import List, Iterable, Dict from collections import defaultdict -from .source import SourceAttribute from .parents import DatabaseObject -from ..utils import string_processing class Collection: """ - This an class for the iterables + This a class for the iterables like tracklist or discography """ - _data: List[SourceAttribute] + _data: List[DatabaseObject] _by_url: dict _by_attribute: dict diff --git a/src/music_kraken/objects/parents.py b/src/music_kraken/objects/parents.py index 0b352a2..e194fcc 100644 --- a/src/music_kraken/objects/parents.py +++ b/src/music_kraken/objects/parents.py @@ -1,3 +1,4 @@ +from collections import defaultdict from typing import Optional, Dict, Type, Tuple, List import uuid @@ -23,6 +24,23 @@ class DatabaseObject: self.id: Optional[str] = _id self.dynamic = dynamic + + def __eq__(self, other) -> bool: + if not isinstance(other, type(self)): + return False + + temp_attribute_map: Dict[str, set] = defaultdict(set) + + # building map with sets + for name, value in self.indexing_values: + temp_attribute_map[name].add(value) + + # check against the attributes of the other object + for name, other_value in other.indexing_values: + if other_value in temp_attribute_map[name]: + return True + + return False @property def indexing_values(self) -> List[Tuple[str, object]]: