Skip to content

Instantly share code, notes, and snippets.

@cosven
Last active December 8, 2020 15:15
Show Gist options
  • Save cosven/c36076b7bbe330e5d2fdef5fbd11604f to your computer and use it in GitHub Desktop.
Save cosven/c36076b7bbe330e5d2fdef5fbd11604f to your computer and use it in GitHub Desktop.
feeluown models v2 - demo
from typing import List
class SongDisplayModel:
pass
class SongModel:
pass
class AlbumModel:
pass
# flags.py
EMPTY = 1 << 0
GET = 1 << 1
BATCH = 1 << 2
SIMILAR_SONG = 1 << 5
# provider.py
class LocalProvider:
# {model_type: flags}
FLAGS = {
'song': GET | BATCH | SIMILAR_SONG,
'albums': GET | BATCH,
}
# self
def check_flags(cls, model_type, flags):
return cls.FlAGS.get(model_type, EMPTY) & flags
def search(self, keyword):
pass
# songs
def get_song(self, identifier) -> SongModel:
pass
def list_songs(self, identifier_list) -> List[SongModel]:
pass
def list_similar_songs(self, song) -> List[SongModel]:
pass
# albums
def get_album(self, identifier) -> AlbumModel:
pass
class SongResolver:
def get_media(self, model):
provider = library.get(model.source)
return provider.Song.get_media(model.identifier)
def p_get_media(self, identifier):
return self._api.get_song_url(identifier)
class Library:
def register(self, provider):
pass
def check_flags(self, model, flags):
"""
:raises: ProviderNotFound
"""
provider = self.get_provider(model.source)
return provider.check_flags(model.type_, flags)
async def async_run(func, *args):
pass
library = Library()
local_provider = LocalProvider()
library.register(local_provider)
def case1():
"""display model and get model attribute"""
# way 1
brief_song = SongDisplayModel(identifier=1, title='x')
title = await async_run(library.Song.get_title, brief_song)
url = await async_run(library.Song.get_url, brief_song)
# way 2
song = await async_run(library.enrich_model, brief_song)
title = song.title
brief_album = song.brief_album
album = await async_run(library.enrich_model, brief_album)
artist = album.brief_artists[0]
def case2():
"""attributes which will be expired"""
# way 1 --- no
media = song.media
if media.is_expired:
song = refresh_song(song)
media = song.media
# way 2 --- yes
media = library.song_list_quality(song)
def case5():
"""models paging"""
reader = app.l.artist_create_songs_reader(artist)
def case4():
"""if allow_xxx, then"""
if library.check_flags(song, SIMILAR_SONG):
similar_songs = library.song_list_similar_songs(song)
def case3():
"""deserialize"""
easy
source, type_, identifier = 'local', 'song', '1'
song = library.get_model(source, type_, identifier)
# case: search
library.search()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment