From b7bd9f6196ce546196bf912c582a03f437bf7bea Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Thu, 9 Mar 2023 18:19:49 +0100 Subject: [PATCH] dsa --- src/music_kraken/objects/collection.py | 56 +++++++++++--------------- src/music_kraken/objects/parents.py | 14 ++++++- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/music_kraken/objects/collection.py b/src/music_kraken/objects/collection.py index 80deb4d..b3e87a2 100644 --- a/src/music_kraken/objects/collection.py +++ b/src/music_kraken/objects/collection.py @@ -1,6 +1,7 @@ -from typing import List, Iterable +from typing import List, Iterable, Dict from .source import SourceAttribute +from .parents import DatabaseObject from ..utils import string_processing @@ -14,20 +15,27 @@ class Collection: _by_url: dict _by_attribute: dict - def __init__(self, data: list = None, map_attributes: list = None, element_type=None) -> None: - """ - Attribute needs to point to - """ - self._by_url = dict() - - self.map_attributes = map_attributes or [] + def __init__(self, data: List[DatabaseObject] = None, element_type = None, *args, **kwargs) -> None: + # Attribute needs to point to self.element_type = element_type - self._by_attribute = {attr: dict() for attr in map_attributes} - - self._data = data or [] - - for element in self._data: - self.map_element(element=element) + + self._data: list = list() + + """ + example of attribute_to_object_map + the song objects are references pointing to objects + in _data + + ```python + { + 'id': {323: song_1, 563: song_2, 666: song_3}, + 'url': {'www.song_2.com': song_2} + } + ``` + """ + self.attribute_to_object_map: Dict[str, dict] = dict() + + self.extend(data, merge_on_conflict=True) def sort(self, reverse: bool = False, **kwargs): self._data.sort(reverse=reverse, **kwargs) @@ -44,24 +52,8 @@ class Collection: self._by_attribute[attr][string_processing.unify(value)] = element - def get_object_with_source(self, url: str) -> any: - """ - Returns either None, or the object, that has a source - matching the url. - """ - if url in self._by_url: - return self._by_url[url] - - def get_object_with_attribute(self, name: str, value: str): - if name not in self.map_attributes: - raise ValueError(f"didn't map the attribute {name}") - - unified = string_processing.unify(value) - if unified in self._by_attribute[name]: - return self._by_attribute[name][unified] - - def append(self, element, merge_on_conflict: bool = True): - if type(element) is not self.element_type and self.element_type is not None: + def append(self, element: DatabaseObject, merge_on_conflict: bool = True): + if self.element_type is not None and isinstance(element, self.element_type): raise TypeError(f"{type(element)} is not the set type {self.element_type}") for source_url in element.source_url_map: diff --git a/src/music_kraken/objects/parents.py b/src/music_kraken/objects/parents.py index 5a0c245..3210228 100644 --- a/src/music_kraken/objects/parents.py +++ b/src/music_kraken/objects/parents.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Optional, Dict, Type import uuid from ..utils.shared import ( @@ -24,6 +24,18 @@ class DatabaseObject: self.dynamic = dynamic + @property + def indexing_values(self) -> Dict[str, object]: + """ + returns a map of the name and values of the attributes. + This helps in comparing classes for equal data (eg. being the same song but different attributes) + + Returns: + Dict[str, object]: the key is the name of the attribute, and the value its value + """ + + return dict() + def merge(self, other, override: bool = False): if isinstance(other, type(self)): LOGGER.warning(f"can't merge \"{type(other)}\" into \"{type(self)}\"")