From bfefc9aaf1c6c7fba692eeba5e01983508956303 Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Tue, 7 Feb 2023 19:26:14 +0100 Subject: [PATCH] hs --- .../database/objects/collection.py | 61 +++++++++++++++++++ src/try_python.py | 19 ------ 2 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 src/music_kraken/database/objects/collection.py diff --git a/src/music_kraken/database/objects/collection.py b/src/music_kraken/database/objects/collection.py new file mode 100644 index 0000000..215c783 --- /dev/null +++ b/src/music_kraken/database/objects/collection.py @@ -0,0 +1,61 @@ +from typing import List + +from .source import SourceAttribute +from ...utils import string_processing + +class Collection: + """ + This an class for the iterables + like tracklist or discography + """ + _data: List[SourceAttribute] + + _by_url: dict + _by_attribute: dict + + + def __init__(self, data: list = None, map_attributes: list = None) -> None: + """ + Attribute needs to point to + """ + self._by_url = dict() + + + self.map_attributes = map_attributes or [] + self._by_attribute = {attr: dict() for attr in map_attributes} + + self._data = data or [] + + for element in self._data: + self.map_element(element=element) + + def map_element(self, element: SourceAttribute): + self._by_url.update(element.source_url_map) + for attr in self.map_attributes: + value = element.__getattribute__(attr) + if type(value) != str: + # this also throws out all none values + continue + + 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][unified]: + return self._by_attribute[name][unified] + + def append(self, element: SourceAttribute): + self._data.append(element) + self.map_element(element) diff --git a/src/try_python.py b/src/try_python.py index a28c681..e69de29 100644 --- a/src/try_python.py +++ b/src/try_python.py @@ -1,19 +0,0 @@ -class AttributeThing: - def __init__(self) -> None: - self.an_attribute = 666 - - def __setattr__(self, __name: str, __value: any) -> None: - print(__name, __value) - self.an_attribute = __value - - def __getattribute__(self, __name: str) -> any: - print(__name) - self.an_attribute += 333 - return self.an_attribute - - -if __name__ == "__main__": - attribute_class = AttributeThing() - - attribute_class.an_attribute = 333 - #print(attribute_class.an_attribute)