From 05453e8f1ac2490502f718228bb02a1c6680650a Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 23 Nov 2023 21:31:29 +0000 Subject: [PATCH] Switch if/elif/else to use pattern matching instead --- agenda/thespacedevs.py | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/agenda/thespacedevs.py b/agenda/thespacedevs.py index f6107a2..b7260a8 100644 --- a/agenda/thespacedevs.py +++ b/agenda/thespacedevs.py @@ -40,28 +40,27 @@ def format_time(time_str: str, net_precision: str) -> tuple[str, str | None]: include_time = False # Format the date based on precision - time_format: str | None = None + time_format: str | None field2 = None - if net_precision == "Year": - time_format = "%Y" - if net_precision == "Month": - time_format = "%b %Y" - if net_precision == "Week": - time_format = "%d %b %Y" - field2 = "(Week of)" - if net_precision == "Day": - time_format = "%d %b %Y" - if net_precision == "Hour": - time_format = "%d %b %Y" - include_time = True - if net_precision == "Minute": - time_format = "%d %b %Y" - include_time = True - elif net_precision == "Second": - time_format = "%d %b %Y" - include_time = True - elif net_precision.startswith("Quarter "): - time_format = f"Q{net_precision[-1]} %Y" + + match net_precision: + case "Year": + time_format = "%Y" + case "Month": + time_format = "%b %Y" + case "Week": + time_format = "%d %b %Y" + field2 = "(Week of)" + case "Day" | "Hour" | "Minute": + time_format = "%d %b %Y" + include_time = net_precision in {"Hour", "Minute"} + case "Second": + time_format = "%d %b %Y" + include_time = True + case _ if net_precision.startswith("Quarter "): + time_format = f"Q{net_precision[-1]} %Y" + case _: + time_format = None if not time_format: return (repr(time_str), repr(net_precision))