Ditch convert_if_exists, just use option_fmap which I prefer

This commit is contained in:
Danielle McLean 2024-07-23 11:03:13 +10:00
parent fda799e32e
commit 1bb2032b9f
Signed by: 00dani
GPG key ID: 6854781A0488421C
2 changed files with 3 additions and 10 deletions

View file

@ -11,7 +11,7 @@ from ..config.model import MpdConfig
from ..player import Player from ..player import Player
from ..song import Artwork, PlaybackState, Song, to_artwork, to_brainz from ..song import Artwork, PlaybackState, Song, to_artwork, to_brainz
from ..song_receiver import Receiver from ..song_receiver import Receiver
from ..tools.types import convert_if_exists, un_maybe_plural from ..tools.types import option_fmap, un_maybe_plural
from .artwork_cache import MpdArtworkCache from .artwork_cache import MpdArtworkCache
from .types import CurrentSongResponse, StatusResponse from .types import CurrentSongResponse, StatusResponse
@ -30,8 +30,8 @@ def mpd_current_to_song(
album_artist=un_maybe_plural(current.get("albumartist")), album_artist=un_maybe_plural(current.get("albumartist")),
composer=un_maybe_plural(current.get("composer")), composer=un_maybe_plural(current.get("composer")),
genre=un_maybe_plural(current.get("genre")), genre=un_maybe_plural(current.get("genre")),
track=convert_if_exists(current.get("track"), int), track=option_fmap(int, current.get("track")),
disc=convert_if_exists(current.get("disc"), int), disc=option_fmap(int, current.get("disc")),
duration=float(status["duration"]), duration=float(status["duration"]),
elapsed=float(status["elapsed"]), elapsed=float(status["elapsed"]),
musicbrainz=to_brainz(current), musicbrainz=to_brainz(current),

View file

@ -5,7 +5,6 @@ __all__ = (
"AnyExceptList", "AnyExceptList",
"MaybePlural", "MaybePlural",
"option_fmap", "option_fmap",
"convert_if_exists",
"un_maybe_plural", "un_maybe_plural",
) )
@ -45,12 +44,6 @@ def option_fmap(f: Callable[[U], V], value: U | None) -> V | None:
return f(value) return f(value)
def convert_if_exists(value: str | None, converter: Callable[[str], U]) -> U | None:
if value is None:
return None
return converter(value)
T = TypeVar("T", bound=AnyExceptList) T = TypeVar("T", bound=AnyExceptList)
MaybePlural: TypeAlias = list[T] | T MaybePlural: TypeAlias = list[T] | T