From 02d358907cd3e1e1a0d897a19e60d636c373c79e Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Mon, 12 Dec 2022 12:46:24 +0100 Subject: [PATCH] Embrace f-strings --- tests/test_console.py | 6 ++--- toot/output.py | 55 +++++++++++++++++++++---------------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/tests/test_console.py b/tests/test_console.py index 5601841..ae61ada 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -567,21 +567,21 @@ def test_notifications(mock_get, capsys): "", "We still have fans in 2017 @fan123", "", - "ID 111111111111111111 ", + "ID 111111111111111111 ", "────────────────────────────────────────────────────────────────────────────────────────────────────", "Terry Bozzio @terry@bozzio.social reblogged your status", "Zappa Fan @fan123@zappa-fans.social 1983-11-04 15:53 UTC", "", "The Black Page, a masterpiece", "", - "ID 1234 ", + "ID 1234 ", "────────────────────────────────────────────────────────────────────────────────────────────────────", "Zappa Old Fan @fan9@zappa-fans.social favourited your status", "Zappa Fan @fan123@zappa-fans.social 1983-11-04 15:53 UTC", "", "The Black Page, a masterpiece", "", - "ID 1234 ", + "ID 1234 ", "────────────────────────────────────────────────────────────────────────────────────────────────────", "", ]) diff --git a/toot/output.py b/toot/output.py index 38b69ec..4927e58 100644 --- a/toot/output.py +++ b/toot/output.py @@ -133,15 +133,15 @@ def print_out(*args, **kwargs): def print_err(*args, **kwargs): - args = ["{}".format(a) for a in args] + args = [f"{a}" for a in args] args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args] print(*args, file=sys.stderr, **kwargs) def print_instance(instance): - print_out("{}".format(instance['title'])) - print_out("{}".format(instance['uri'])) - print_out("running Mastodon {}".format(instance['version'])) + print_out(f"{instance['title']}") + print_out(f"{instance['uri']}") + print_out(f"running Mastodon {instance['version']}") print_out() description = instance.get("description") @@ -167,7 +167,7 @@ def print_instance(instance): def print_account(account): - print_out("@{} {}".format(account['acct'], account['display_name'])) + print_out(f"@{account['acct']} {account['display_name']}") note = get_text(account['note']) @@ -176,14 +176,14 @@ def print_account(account): print_out("\n".join(wrap(note))) print_out("") - print_out("ID: {}".format(account['id'])) - print_out("Since: {}".format(account['created_at'][:10])) + print_out(f"ID: {account['id']}") + print_out(f"Since: {account['created_at'][:10]}") print_out("") - print_out("Followers: {}".format(account['followers_count'])) - print_out("Following: {}".format(account['following_count'])) - print_out("Statuses: {}".format(account['statuses_count'])) + print_out(f"Followers: {account['followers_count']}") + print_out(f"Following: {account['following_count']}") + print_out(f"Statuses: {account['statuses_count']}") print_out("") - print_out(account['url']) + print_out(account["url"]) HASHTAG_PATTERN = re.compile(r'(?@{} {}".format( - account['acct'], - account['display_name'] - )) + print_out(f"* @{account['acct']} {account['display_name']}") def print_search_results(results): @@ -211,7 +208,7 @@ def print_search_results(results): if hashtags: print_out("\nHashtags:") - print_out(", ".join(["#{}".format(t["name"]) for t in hashtags])) + print_out(", ".join([f"#{t['name']}" for t in hashtags])) if not accounts and not hashtags: print_out("Nothing found") @@ -228,18 +225,18 @@ def print_status(status, width): time = time.strftime('%Y-%m-%d %H:%M %Z') username = "@" + status['account']['acct'] - spacing = width - wcswidth(username) - wcswidth(time) + spacing = width - wcswidth(username) - wcswidth(time) - 2 display_name = status['account']['display_name'] if display_name: spacing -= wcswidth(display_name) + 1 - print_out("{}{}{}{}".format( - "{} ".format(display_name) if display_name else "", - "{}".format(username), + print_out( + f"{display_name}" if display_name else "", + f"{username}", " " * spacing, - "{}".format(time), - )) + f"{time}", + ) for paragraph in parse_html(content): print_out("") @@ -257,11 +254,12 @@ def print_status(status, width): if poll: print_poll(poll) - print_out("\n{}{}{}".format( - "ID {} ".format(status['id']), - "↲ In reply to {} ".format(in_reply_to) if in_reply_to else "", - "↻ Reblogged @{} ".format(reblog['account']['acct']) if reblog else "", - )) + print_out() + print_out( + f"ID {status['id']} ", + f"↲ In reply to {in_reply_to} " if in_reply_to else "", + f"↻ Reblogged @{reblog['account']['acct']} " if reblog else "", + ) def print_poll(poll): @@ -286,7 +284,8 @@ def print_poll(poll): expires_at = parse_datetime(poll["expires_at"]).strftime("%Y-%m-%d %H:%M") poll_footer += f" · Closes on {expires_at}" - print_out("\n{}".format(poll_footer)) + print_out() + print_out(poll_footer) def print_timeline(items, width=100):