Last active
December 23, 2021 17:22
-
-
Save ryantuck/168a52a1dab6ab3ecf941c3960ff73c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel | |
from typing import List | |
class InputItem(BaseModel): | |
song_id: str | |
audio_filename: str | |
class OutputItem(BaseModel): | |
song_id: str | |
audio_filenames: list # of strs | |
data_in = [ | |
{ | |
'song_id': i % 2, | |
'audio_filename': f'track-{i}.mp3', | |
} | |
for i in range(5) | |
] | |
input_items = [InputItem.parse_obj(song_dict) for song_dict in data_in] | |
def output_items(input_items: List[InputItem]) -> List[OutputItem]: | |
song_ids = set(item.song_id for item in input_items) | |
return [ | |
OutputItem( | |
song_id=song_id, | |
audio_filenames=[ | |
item.audio_filename | |
for item in input_items | |
if item.song_id == song_id | |
], | |
) | |
for song_id in song_ids | |
] | |
print('INPUT ITEMS') | |
for ii in input_items: | |
print(ii.json(indent=2)) | |
print('OUTPUT ITEMS') | |
for oi in output_items(input_items): | |
print(oi.json(indent=2)) |
Author
ryantuck
commented
Dec 23, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment