From d64f354ee09884ff7877725c6764b209e13dfdd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Wed, 8 Mar 2023 21:42:47 +0800 Subject: [PATCH] Added the DATE_SPEC_RE constant to simplify the regular expression matching in the _parse_period_spec function. --- src/accounting/report/period.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/accounting/report/period.py b/src/accounting/report/period.py index a57a2bc..35ff29c 100644 --- a/src/accounting/report/period.py +++ b/src/accounting/report/period.py @@ -554,6 +554,10 @@ class YearPeriod(Period): self.is_a_year = True +DATE_SPEC_RE: str = r"(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?" +"""The regular expression of a date specification.""" + + def _parse_period_spec(text: str) \ -> tuple[datetime.date | None, datetime.date | None]: """Parses the period specification. @@ -565,17 +569,17 @@ def _parse_period_spec(text: str) \ """ if text == "-": return None, None - m = re.match(r"^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$", text) + m = re.match(f"^{DATE_SPEC_RE}$", text) if m is not None: return __get_start(m[1], m[2], m[3]), \ __get_end(m[1], m[2], m[3]) - m = re.match(r"^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?-$", text) + m = re.match(f"^{DATE_SPEC_RE}-$", text) if m is not None: return __get_start(m[1], m[2], m[3]), None - m = re.match(r"-(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$", text) + m = re.match(f"-{DATE_SPEC_RE}$", text) if m is not None: return None, __get_end(m[1], m[2], m[3]) - m = re.match(r"^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?-(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$", text) + m = re.match(f"^{DATE_SPEC_RE}-{DATE_SPEC_RE}$", text) if m is not None: return __get_start(m[1], m[2], m[3]), \ __get_end(m[4], m[5], m[6])