Switch if/elif/else to use pattern matching instead

This commit is contained in:
Edward Betts 2023-11-23 21:31:29 +00:00
parent 44fcf46178
commit 05453e8f1a

View file

@ -40,28 +40,27 @@ def format_time(time_str: str, net_precision: str) -> tuple[str, str | None]:
include_time = False include_time = False
# Format the date based on precision # Format the date based on precision
time_format: str | None = None time_format: str | None
field2 = None field2 = None
if net_precision == "Year":
time_format = "%Y" match net_precision:
if net_precision == "Month": case "Year":
time_format = "%b %Y" time_format = "%Y"
if net_precision == "Week": case "Month":
time_format = "%d %b %Y" time_format = "%b %Y"
field2 = "(Week of)" case "Week":
if net_precision == "Day": time_format = "%d %b %Y"
time_format = "%d %b %Y" field2 = "(Week of)"
if net_precision == "Hour": case "Day" | "Hour" | "Minute":
time_format = "%d %b %Y" time_format = "%d %b %Y"
include_time = True include_time = net_precision in {"Hour", "Minute"}
if net_precision == "Minute": case "Second":
time_format = "%d %b %Y" time_format = "%d %b %Y"
include_time = True include_time = True
elif net_precision == "Second": case _ if net_precision.startswith("Quarter "):
time_format = "%d %b %Y" time_format = f"Q{net_precision[-1]} %Y"
include_time = True case _:
elif net_precision.startswith("Quarter "): time_format = None
time_format = f"Q{net_precision[-1]} %Y"
if not time_format: if not time_format:
return (repr(time_str), repr(net_precision)) return (repr(time_str), repr(net_precision))