Replace slightly more advanced attempts to use format calls

This commit is contained in:
Šarūnas Nejus 2025-08-30 18:14:46 +01:00
parent 9352a79e41
commit a7c83d91e9
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
7 changed files with 16 additions and 23 deletions

View file

@ -252,7 +252,8 @@ There are a few coding conventions we use in beets:
Transaction objects help control concurrent access to the database and assist
in debugging conflicting accesses.
- ``str.format()`` should be used instead of the ``%`` operator
- f-strings should be used instead of the ``%`` operator and ``str.format()``
calls.
- Never ``print`` informational messages; use the `logging
<http://docs.python.org/library/logging.html>`__ module instead. In
particular, we have our own logging shim, so youll see ``from beets import

View file

@ -848,8 +848,6 @@ class DateQuery(FieldQuery[str]):
date = datetime.fromtimestamp(timestamp)
return self.interval.contains(date)
_clause_tmpl = "{0} {1} ?"
def col_clause(self) -> tuple[str, Sequence[SQLiteType]]:
clause_parts = []
subvals = []
@ -857,11 +855,11 @@ class DateQuery(FieldQuery[str]):
# Convert the `datetime` objects to an integer number of seconds since
# the (local) Unix epoch using `datetime.timestamp()`.
if self.interval.start:
clause_parts.append(self._clause_tmpl.format(self.field, ">="))
clause_parts.append(f"{self.field} >= ?")
subvals.append(int(self.interval.start.timestamp()))
if self.interval.end:
clause_parts.append(self._clause_tmpl.format(self.field, "<"))
clause_parts.append(f"{self.field} < ?")
subvals.append(int(self.interval.end.timestamp()))
if clause_parts:

View file

@ -486,7 +486,6 @@ class ChangeRepresentation:
"""Format colored track indices."""
cur_track = self.format_index(item)
new_track = self.format_index(track_info)
templ = "(#{})"
changed = False
# Choose color based on change.
if cur_track != new_track:
@ -498,10 +497,8 @@ class ChangeRepresentation:
else:
highlight_color = "text_faint"
cur_track = templ.format(cur_track)
new_track = templ.format(new_track)
lhs_track = ui.colorize(highlight_color, cur_track)
rhs_track = ui.colorize(highlight_color, new_track)
lhs_track = ui.colorize(highlight_color, f"(#{cur_track})")
rhs_track = ui.colorize(highlight_color, f"(#{new_track})")
return lhs_track, rhs_track, changed
@staticmethod

View file

@ -150,7 +150,7 @@ class DuplicatesPlugin(BeetsPlugin):
count = self.config["count"].get(bool)
delete = self.config["delete"].get(bool)
remove = self.config["remove"].get(bool)
fmt = self.config["format"].get(str)
fmt_tmpl = self.config["format"].get(str)
full = self.config["full"].get(bool)
keys = self.config["keys"].as_str_seq()
merge = self.config["merge"].get(bool)
@ -175,15 +175,14 @@ class DuplicatesPlugin(BeetsPlugin):
return
if path:
fmt = "$path"
fmt_tmpl = "$path"
# Default format string for count mode.
if count and not fmt:
if count and not fmt_tmpl:
if album:
fmt = "$albumartist - $album"
fmt_tmpl = "$albumartist - $album"
else:
fmt = "$albumartist - $album - $title"
fmt += ": {0}"
fmt_tmpl = "$albumartist - $album - $title"
if checksum:
for i in items:
@ -207,7 +206,7 @@ class DuplicatesPlugin(BeetsPlugin):
delete=delete,
remove=remove,
tag=tag,
fmt=fmt.format(obj_count),
fmt=f"{fmt_tmpl}: {obj_count}",
)
self._command.func = _dup

View file

@ -117,7 +117,6 @@ def print_data(data, item=None, fmt=None):
return
maxwidth = max(len(key) for key in formatted)
lineformat = f"{{0:>{maxwidth}}}: {{1}}"
if path:
ui.print_(displayable_path(path))
@ -126,7 +125,7 @@ def print_data(data, item=None, fmt=None):
value = formatted[field]
if isinstance(value, list):
value = "; ".join(value)
ui.print_(lineformat.format(field, value))
ui.print_(f"{field:>{maxwidth}}: {value}")
def print_data_keys(data, item=None):
@ -139,12 +138,11 @@ def print_data_keys(data, item=None):
if len(formatted) == 0:
return
line_format = "{0}{{0}}".format(" " * 4)
if path:
ui.print_(displayable_path(path))
for field in sorted(formatted):
ui.print_(line_format.format(field))
ui.print_(f" {field}")
class InfoPlugin(BeetsPlugin):

View file

@ -36,7 +36,8 @@ def _compile_func(body):
"""Given Python code for a function body, return a compiled
callable that invokes that code.
"""
body = "def {}():\n {}".format(FUNC_NAME, body.replace("\n", "\n "))
body = body.replace("\n", "\n ")
body = f"def {FUNC_NAME}():\n {body}"
code = compile(body, "inline", "exec")
env = {}
eval(code, env)

View file

@ -1261,7 +1261,6 @@ class ShowChangeTest(IOMixin, unittest.TestCase):
msg = self._show_change(
cur_artist=long_name, cur_album="another album"
)
# _common.log.info("Message:{}".format(msg))
assert "artist: another artist" in msg
assert " -> the artist" in msg
assert "another album -> the album" not in msg