made a function to compile

This commit is contained in:
Hellow2 2023-03-14 11:03:54 +01:00
parent 38142df92e
commit 4056c736b9
7 changed files with 137 additions and 1 deletions

View File

@ -1,4 +1,4 @@
from music_kraken import objects from music_kraken import objects, recurse
import pycountry import pycountry
@ -41,6 +41,9 @@ song = objects.Song(
objects.SourcePages.ENCYCLOPAEDIA_METALLUM, objects.SourcePages.ENCYCLOPAEDIA_METALLUM,
"https://www.metal-archives.com/bands/I%27m_in_a_Coffin/127727" "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") objects.Artist(name="some_split_artist")
@ -55,6 +58,8 @@ song = objects.Song(
], ],
) )
song.compile()
print(song.option_string) print(song.option_string)
for album in song.album_collection: for album in song.album_collection:
print(album.option_string) print(album.option_string)

View File

@ -33,6 +33,7 @@ class Collection:
``` ```
""" """
self._attribute_to_object_map: Dict[str, Dict[object, DatabaseObject]] = defaultdict(dict) self._attribute_to_object_map: Dict[str, Dict[object, DatabaseObject]] = defaultdict(dict)
self._used_ids: set = set()
if data is not None: if data is not None:
self.extend(data, merge_on_conflict=True) self.extend(data, merge_on_conflict=True)
@ -47,6 +48,8 @@ class Collection:
self._attribute_to_object_map[name][value] = element self._attribute_to_object_map[name][value] = element
self._used_ids.add(element.id)
def append(self, element: DatabaseObject, merge_on_conflict: bool = True): def append(self, element: DatabaseObject, merge_on_conflict: bool = True):
""" """
:param element: :param element:
@ -98,3 +101,21 @@ class Collection:
returns a shallow copy of the data list returns a shallow copy of the data list
""" """
return self._data.copy() 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

View File

@ -83,6 +83,18 @@ class DatabaseObject:
def option_string(self) -> str: def option_string(self) -> str:
return self.__repr__() 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): class MainObject(DatabaseObject):
""" """

View File

@ -83,6 +83,21 @@ class Song(MainObject):
self.main_artist_collection = Collection(data=main_artist_list, element_type=Artist) self.main_artist_collection = Collection(data=main_artist_list, element_type=Artist)
self.feature_artist_collection = Collection(data=feature_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 @property
def indexing_values(self) -> List[Tuple[str, object]]: def indexing_values(self) -> List[Tuple[str, object]]:
return [ return [
@ -214,6 +229,24 @@ class Album(MainObject):
self.artist_collection: Collection = Collection(data=artist_list, element_type=Artist) self.artist_collection: Collection = Collection(data=artist_list, element_type=Artist)
self.label_collection: Collection = Collection(data=label_list, element_type=Label) 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 @property
def indexing_values(self) -> List[Tuple[str, object]]: def indexing_values(self) -> List[Tuple[str, object]]:
return [ return [
@ -365,6 +398,22 @@ class Artist(MainObject):
self.main_album_collection: Collection = Collection(data=main_album_list, element_type=Album) self.main_album_collection: Collection = Collection(data=main_album_list, element_type=Album)
self.label_collection: Collection = Collection(data=label_list, element_type=Label) 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 @property
def indexing_values(self) -> List[Tuple[str, object]]: def indexing_values(self) -> List[Tuple[str, object]]:
return [ return [
@ -485,6 +534,17 @@ class Label(MainObject):
self.album_collection: Collection = Collection(data=album_list, element_type=Album) self.album_collection: Collection = Collection(data=album_list, element_type=Album)
self.current_artist_collection: Collection = Collection(data=current_artist_list, element_type=Artist) 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 @property
def indexing_values(self) -> List[Tuple[str, object]]: def indexing_values(self) -> List[Tuple[str, object]]:
return [ return [

View File

@ -0,0 +1,3 @@
from . import build
Builder = build.Builder

View File

@ -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())