From f98887297f4766d0ad8f1b49c9795086d4a6d02a Mon Sep 17 00:00:00 2001 From: J0J0 Todos Date: Sun, 12 Jan 2025 11:07:35 +0100 Subject: [PATCH] Simplify _last_lookup() f-string, list comprehension, remove redundant vars. --- beetsplug/lastgenre/__init__.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index c6824e5628..443e64ba5f 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -277,29 +277,29 @@ def _is_valid(self, genre) -> bool: def _last_lookup(self, entity, method, *args): """Get a genre based on the named entity using the callable `method` whose arguments are given in the sequence `args`. The genre lookup - is cached based on the entity name and the arguments. Before the - lookup, each argument is has some Unicode characters replaced with - rough ASCII equivalents in order to return better results from the + is cached based on the entity name and the arguments. + + Before the lookup, each argument has some Unicode characters replaced + with rough ASCII equivalents in order to return better results from the Last.fm database. """ # Shortcut if we're missing metadata. if any(not s for s in args): return None - key = "{}.{}".format(entity, "-".join(str(a) for a in args)) + key = f"{entity}.{"-".join(str(a) for a in args)}" + if key in self._genre_cache: - result = self._genre_cache[key] - else: - args_replaced = [] - for arg in args: - for k, v in REPLACE.items(): - arg = arg.replace(k, v) - args_replaced.append(arg) - - genre = self.fetch_genre(method(*args_replaced)) - self._genre_cache[key] = genre - result = genre - return result + return self._genre_cache[key] + + args_replaced = [ + ''.join(arg.replace(k, v) for k, v in REPLACE.items()) + for arg in args + ] + self._genre_cache[key] = self.fetch_genre(method(*args_replaced)) + + return self._genre_cache[key] + def fetch_album_genre(self, obj): """Return the album genre for this Item or Album."""