From 4056c736b9dcfbbce13956ff9cf6d9c6cb66a273 Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Tue, 14 Mar 2023 11:03:54 +0100 Subject: [PATCH] made a function to compile --- src/create_custom_objects.py | 7 ++- src/music_kraken/objects/collection.py | 21 +++++++ src/music_kraken/objects/parents.py | 12 ++++ src/music_kraken/objects/song.py | 60 ++++++++++++++++++++ src/music_kraken/recurse/__init__.py | 3 + src/music_kraken/recurse/build.py | 35 ++++++++++++ src/music_kraken/recurse_objects/__init__.py | 0 7 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 src/music_kraken/recurse/__init__.py create mode 100644 src/music_kraken/recurse/build.py delete mode 100644 src/music_kraken/recurse_objects/__init__.py diff --git a/src/create_custom_objects.py b/src/create_custom_objects.py index 3a9c323..1312d19 100644 --- a/src/create_custom_objects.py +++ b/src/create_custom_objects.py @@ -1,4 +1,4 @@ -from music_kraken import objects +from music_kraken import objects, recurse import pycountry @@ -41,6 +41,9 @@ song = objects.Song( objects.SourcePages.ENCYCLOPAEDIA_METALLUM, "https://www.metal-archives.com/bands/I%27m_in_a_Coffin/127727" ) + ], + label_list=[ + objects.Label(name="Depressive records") ] ), objects.Artist(name="some_split_artist") @@ -55,6 +58,8 @@ song = objects.Song( ], ) +song.compile() + print(song.option_string) for album in song.album_collection: print(album.option_string) diff --git a/src/music_kraken/objects/collection.py b/src/music_kraken/objects/collection.py index 0173de0..d70617b 100644 --- a/src/music_kraken/objects/collection.py +++ b/src/music_kraken/objects/collection.py @@ -33,6 +33,7 @@ class Collection: ``` """ self._attribute_to_object_map: Dict[str, Dict[object, DatabaseObject]] = defaultdict(dict) + self._used_ids: set = set() if data is not None: self.extend(data, merge_on_conflict=True) @@ -46,6 +47,8 @@ class Collection: continue self._attribute_to_object_map[name][value] = element + + self._used_ids.add(element.id) def append(self, element: DatabaseObject, merge_on_conflict: bool = True): """ @@ -98,3 +101,21 @@ class Collection: returns a shallow copy of the data list """ return self._data.copy() + + def insecure_append(self, element: DatabaseObject): + if element.id in self._used_ids: + return False + self._used_ids.add(element.id) + + self._data.append(element) + self.map_element(element) + return True + + def insecure_extend(self, element_list: Iterable[DatabaseObject]): + success = False + + for element in element_list: + if self.insecure_append(element): + success = True + + return success diff --git a/src/music_kraken/objects/parents.py b/src/music_kraken/objects/parents.py index c9c5ca9..fbb2993 100644 --- a/src/music_kraken/objects/parents.py +++ b/src/music_kraken/objects/parents.py @@ -82,6 +82,18 @@ class DatabaseObject: @property def option_string(self) -> str: return self.__repr__() + + def compile(self) -> bool: + """ + compiles the recursive structures, + + Args: + traceback (set, optional): Defaults to an empty set. + + Returns: + bool: returns true if id has been found in set + """ + pass class MainObject(DatabaseObject): diff --git a/src/music_kraken/objects/song.py b/src/music_kraken/objects/song.py index 53769fa..252678d 100644 --- a/src/music_kraken/objects/song.py +++ b/src/music_kraken/objects/song.py @@ -83,6 +83,21 @@ class Song(MainObject): self.main_artist_collection = Collection(data=main_artist_list, element_type=Artist) self.feature_artist_collection = Collection(data=feature_artist_list, element_type=Artist) + def compile(self): + album: Album + for album in self.album_collection: + if album.song_collection.insecure_append(self): + album.compile() + + artist: Artist + for artist in self.feature_artist_collection: + if artist.feature_song_collection.insecure_append(self): + artist.compile() + + for artist in self.main_artist_collection: + if artist.main_album_collection.insecure_extend(self.album_collection): + artist.compile() + @property def indexing_values(self) -> List[Tuple[str, object]]: return [ @@ -214,6 +229,24 @@ class Album(MainObject): self.artist_collection: Collection = Collection(data=artist_list, element_type=Artist) self.label_collection: Collection = Collection(data=label_list, element_type=Label) + def compile(self): + song: "Song" + for song in self.song_collection: + if song.album_collection.insecure_append(self): + song.compile() + + artist: Artist + for artist in self.artist_collection: + if artist.main_album_collection.insecure_append(self): + artist.compile() + + label: Label + for label in self.label_collection: + if label.album_collection.insecure_append(self): + label.compile() + + + @property def indexing_values(self) -> List[Tuple[str, object]]: return [ @@ -365,6 +398,22 @@ class Artist(MainObject): self.main_album_collection: Collection = Collection(data=main_album_list, element_type=Album) self.label_collection: Collection = Collection(data=label_list, element_type=Label) + def compile(self): + song: "Song" + for song in self.feature_song_collection: + if song.feature_artist_collection.insecure_append(self): + song.compile() + + album: "Album" + for album in self.main_album_collection: + if album.artist_collection.insecure_append(self): + album.compile() + + label: Label + for label in self.label_collection: + if label.current_artist_collection.insecure_append(self): + label.compile() + @property def indexing_values(self) -> List[Tuple[str, object]]: return [ @@ -485,6 +534,17 @@ class Label(MainObject): self.album_collection: Collection = Collection(data=album_list, element_type=Album) self.current_artist_collection: Collection = Collection(data=current_artist_list, element_type=Artist) + def compile(self) -> bool: + album: Album + for album in self.album_collection: + if album.label_collection.insecure_append(self): + album.compile() + + artist: Artist + for artist in self.current_artist_collection: + if artist.label_collection.insecure_append(self): + artist.compile() + @property def indexing_values(self) -> List[Tuple[str, object]]: return [ diff --git a/src/music_kraken/recurse/__init__.py b/src/music_kraken/recurse/__init__.py new file mode 100644 index 0000000..98e44a1 --- /dev/null +++ b/src/music_kraken/recurse/__init__.py @@ -0,0 +1,3 @@ +from . import build + +Builder = build.Builder diff --git a/src/music_kraken/recurse/build.py b/src/music_kraken/recurse/build.py new file mode 100644 index 0000000..4f77447 --- /dev/null +++ b/src/music_kraken/recurse/build.py @@ -0,0 +1,35 @@ +from .. import objects + +class Builder: + @classmethod + def build_album(cls, album: objects.Album, traceback: set): + print(album.option_string) + if objects.Album in traceback: + return + traceback.add(objects.Album) + + for song in album.song_collection: + song.album_collection.append(album) + + @classmethod + def build_song(cls, song: objects.Song, traceback: set): + print(song.option_string) + if objects.Song in traceback: + return + traceback.add(objects.Song) + + for album in song.album_collection: + album.song_collection.append(song) + cls.build_album(album, traceback) + + for feature_artist in song.feature_artist_collection: + feature_artist.feature_song_collection.append(song) + + @classmethod + def build(cls, data_object: objects.MusicObject): + if isinstance(data_object, objects.Song): + cls.build_song(data_object, set()) + + if isinstance(data_object, objects.Album): + cls.build_album(data_object, set()) + \ No newline at end of file diff --git a/src/music_kraken/recurse_objects/__init__.py b/src/music_kraken/recurse_objects/__init__.py deleted file mode 100644 index e69de29..0000000