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

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

View File

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

View File

@@ -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 [