Skip to content

Commit

Permalink
Simplify _last_lookup()
Browse files Browse the repository at this point in the history
f-string, list comprehension, remove redundant vars.
  • Loading branch information
JOJ0 committed Jan 12, 2025
1 parent 1e14904 commit f988872
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions beetsplug/lastgenre/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"

Check failure on line 290 in beetsplug/lastgenre/__init__.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

f-string: expecting '}'

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."""
Expand Down

0 comments on commit f988872

Please sign in to comment.