From 56a9d565c1483e37c01336352dfa7de67ed9631e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 5 Dec 2022 19:21:37 +0800 Subject: [PATCH] Replaced "[0-9]" with \d in the regular expressions. --- src/accounting/converters.py | 8 +++---- src/accounting/forms.py | 10 ++++---- src/accounting/models.py | 6 ++--- .../static/accounting/js/summary-helper.js | 6 ++--- .../static/accounting/js/transaction-form.js | 4 ++-- src/accounting/templatetags/accounting.py | 4 ++-- src/accounting/views.py | 4 ++-- src/mia_core/period.py | 24 +++++++++---------- 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/accounting/converters.py b/src/accounting/converters.py index c5a7ec2..1a05b33 100644 --- a/src/accounting/converters.py +++ b/src/accounting/converters.py @@ -40,9 +40,9 @@ class TransactionTypeConverter: class PeriodConverter: """The path converter for the period.""" - regex = ("([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)|" - "([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)?-" - "([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)?") + regex = (r"([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)|" + r"([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)?-" + r"([0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?)?") def to_python(self, value): """Returns the period by the period specification. @@ -90,7 +90,7 @@ class DateConverter: Returns: datetime.date: The date. """ - m = re.match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$", value) + m = re.match(r"^(\d{4})-(\d{2})-(\d{2})$", value) year = int(m.group(1)) month = int(m.group(2)) day = int(m.group(3)) diff --git a/src/accounting/forms.py b/src/accounting/forms.py index b84e4be..1af57f5 100644 --- a/src/accounting/forms.py +++ b/src/accounting/forms.py @@ -205,8 +205,8 @@ class TransactionForm(forms.Form): by_rec_id = {} for key in args[0].keys(): m = re.match( - ("^((debit|credit)-([1-9][0-9]*))-" - "(id|ord|account|summary|amount)$"), + (r"^((debit|credit)-([1-9]\d*))-" + r"(id|ord|account|summary|amount)$"), key) if m is None: continue @@ -271,7 +271,7 @@ class TransactionForm(forms.Form): } for key in post.keys(): m = re.match( - "^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)", + r"^(debit|credit)-([1-9]\d*)-(id|ord|account|summary|amount)", key) if m is None: continue @@ -303,7 +303,7 @@ class TransactionForm(forms.Form): = post[F"{record_type}-{old_no}-{attr}"] # Purges the old form and fills it with the new form for x in [x for x in post.keys() if re.match( - "^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)", + r"^(debit|credit)-([1-9]\d*)-(id|ord|account|summary|amount)", x)]: del post[x] for key in new_post.keys(): @@ -475,7 +475,7 @@ class TransactionSortForm(forms.Form): key = F"transaction-{txn.pk}-ord" if key not in post: post_orders.append(form.Order(txn, 9999)) - elif not re.match("^[0-9]+$", post[key]): + elif not re.match(r"^\d+$", post[key]): post_orders.append(form.Order(txn, 9999)) else: post_orders.append(form.Order(txn, int(post[key]))) diff --git a/src/accounting/models.py b/src/accounting/models.py index a1493e5..fd6453b 100644 --- a/src/accounting/models.py +++ b/src/accounting/models.py @@ -216,7 +216,7 @@ class Transaction(DirtyFieldsMixin, StampedModel, RandomPkModel): txn_type: The transaction type. """ self.old_date = self.date - m = re.match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$", post["date"]) + m = re.match(r"^(\d{4})-(\d{2})-(\d{2})$", post["date"]) self.date = datetime.date( int(m.group(1)), int(m.group(2)), @@ -282,8 +282,8 @@ class Transaction(DirtyFieldsMixin, StampedModel, RandomPkModel): max_no["credit"] = 0 for key in post.keys(): m = re.match( - ("^(debit|credit)-([1-9][0-9]*)-" - "(id|ord|account|summary|amount)$"), + (r"^(debit|credit)-([1-9]\d*)-" + r"(id|ord|account|summary|amount)$"), key) if m is None: continue diff --git a/src/accounting/static/accounting/js/summary-helper.js b/src/accounting/static/accounting/js/summary-helper.js index c12c912..510bc40 100644 --- a/src/accounting/static/accounting/js/summary-helper.js +++ b/src/accounting/static/accounting/js/summary-helper.js @@ -329,7 +329,7 @@ function parseSummaryForCategoryHelpers(summary) { }); // A bus route - const matchBus = summary.match(/^([^—]+)—([^—]+)—([^—]+)→([^—]+?)(?:×[0-9]+)?$/); + const matchBus = summary.match(/^([^—]+)—([^—]+)—([^—]+)→([^—]+?)(?:×\d+)?$/); if (matchBus !== null) { $("#summary-bus-category").get(0).value = matchBus[1]; setSummaryBusCategoryButtons(matchBus[1]); @@ -342,7 +342,7 @@ function parseSummaryForCategoryHelpers(summary) { } // A general travel route - const matchTravel = summary.match(/^([^—]+)—([^—]+)([→|↔])([^—]+?)(?:×[0-9]+)?$/); + const matchTravel = summary.match(/^([^—]+)—([^—]+)([→|↔])([^—]+?)(?:×\d+)?$/); if (matchTravel !== null) { $("#summary-travel-category").get(0).value = matchTravel[1]; setSummaryTravelCategoryButtons(matchTravel[1]); @@ -357,7 +357,7 @@ function parseSummaryForCategoryHelpers(summary) { // A general category const generalCategoryTab = $("#summary-tab-category"); - const matchCategory = summary.match(/^([^—]+)—.+(?:×[0-9]+)?$/); + const matchCategory = summary.match(/^([^—]+)—.+(?:×\d+)?$/); if (matchCategory !== null) { $("#summary-general-category").get(0).value = matchCategory[1]; setSummaryGeneralCategoryButtons(matchCategory[1]); diff --git a/src/accounting/static/accounting/js/transaction-form.js b/src/accounting/static/accounting/js/transaction-form.js index 0e1b6d8..b9eaba9 100644 --- a/src/accounting/static/accounting/js/transaction-form.js +++ b/src/accounting/static/accounting/js/transaction-form.js @@ -184,8 +184,8 @@ function updateTotalAmount(element) { } }); total = String(total); - while (total.match(/^[1-9][0-9]*[0-9]{3}/)) { - total = total.replace(/^([1-9][0-9]*)([0-9]{3})/, "$1,$2"); + while (total.match(/^[1-9]\d*\d{3}/)) { + total = total.replace(/^([1-9]\d*)(\d{3})/, "$1,$2"); } $("#" + type + "-total").text(total); } diff --git a/src/accounting/templatetags/accounting.py b/src/accounting/templatetags/accounting.py index 288ef66..4b0b182 100644 --- a/src/accounting/templatetags/accounting.py +++ b/src/accounting/templatetags/accounting.py @@ -42,7 +42,7 @@ def _strip_decimal_zeros(value: Decimal) -> str: str: The value with excess decimal zeros stripped. """ s = str(value) - s = re.sub(r"^(.*\.[0-9]*?)0+$", r"\1", s) + s = re.sub(r"^(.*\.\d*?)0+$", r"\1", s) s = re.sub(r"^(.*)\.$", r"\1", s) return s @@ -58,7 +58,7 @@ def _format_positive_amount(value: Decimal) -> str: """ s = _strip_decimal_zeros(value) while True: - m = re.match("^([1-9][0-9]*)([0-9]{3}.*)", s) + m = re.match(r"^([1-9]\d*)(\d{3}.*)", s) if m is None: break s = m.group(1) + "," + m.group(2) diff --git a/src/accounting/views.py b/src/accounting/views.py index 4199372..4de2185 100644 --- a/src/accounting/views.py +++ b/src/accounting/views.py @@ -804,9 +804,9 @@ class SearchListView(TemplateView): | Q(code=term)))\ | Q(summary__icontains=term)\ | Q(transaction__notes__icontains=term) - if re.match("^[0-9]+(?:\\.[0-9]+)?$", term): + if re.match(r"^\d+(?:\.\d+)?$", term): conditions = conditions | Q(amount=Decimal(term)) - if re.match("^[1-9][0-8]{9}$", term): + if re.match(r"^[1-9][0-8]{9}$", term): conditions = conditions\ | Q(pk=int(term))\ | Q(transaction__pk=int(term))\ diff --git a/src/mia_core/period.py b/src/mia_core/period.py index 89b5d3c..4c78234 100644 --- a/src/mia_core/period.py +++ b/src/mia_core/period.py @@ -195,7 +195,7 @@ class Period: """ if self._data_start is None: return None - m = re.match("^[0-9]{4}-[0-2]{2}", self._period.spec) + m = re.match(r"^\d{4}-\d{2}", self._period.spec) if m is None: return None if self._period.end < self._data_start: @@ -379,9 +379,9 @@ class Period: if self.start <= self._data_start: return None previous_day = self.start - datetime.timedelta(days=1) - if re.match("^[0-9]{4}$", self.spec): + if re.match(r"^\d{4}$", self.spec): return "-" + str(previous_day.year) - if re.match("^[0-9]{4}-[0-9]{2}$", self.spec): + if re.match(r"^\d{4}-\d{2}$", self.spec): return dateformat.format(previous_day, "-Y-m") return dateformat.format(previous_day, "-Y-m-d") @@ -441,7 +441,7 @@ class Period: return self.spec = spec # A specific month - m = re.match("^([0-9]{4})-([0-9]{2})$", spec) + m = re.match(r"^(\d{4})-(\d{2})$", spec) if m is not None: year = int(m.group(1)) month = int(m.group(2)) @@ -452,7 +452,7 @@ class Period: self.prep_desc = gettext("In %s") % self.description return # From a specific month - m = re.match("^([0-9]{4})-([0-9]{2})-$", spec) + m = re.match(r"^(\d{4})-(\d{2})-$", spec) if m is not None: year = int(m.group(1)) month = int(m.group(2)) @@ -464,7 +464,7 @@ class Period: self.prep_desc = self.description return # Until a specific month - m = re.match("^-([0-9]{4})-([0-9]{2})$", spec) + m = re.match(r"^-(\d{4})-(\d{2})$", spec) if m is not None: year = int(m.group(1)) month = int(m.group(2)) @@ -477,7 +477,7 @@ class Period: self.prep_desc = self.description return # A specific year - m = re.match("^([0-9]{4})$", spec) + m = re.match(r"^(\d{4})$", spec) if m is not None: year = int(m.group(1)) # Raises ValueError @@ -487,7 +487,7 @@ class Period: self.prep_desc = gettext("In %s") % self.description return # Until a specific year - m = re.match("^-([0-9]{4})$", spec) + m = re.match(r"^-(\d{4})$", spec) if m is not None: year = int(m.group(1)) # Raises ValueError @@ -505,7 +505,7 @@ class Period: self.prep_desc = gettext("In %s") % self.description return # A specific date - m = re.match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$", + m = re.match(r"^(\d{4})-(\d{2})-(\d{2})$", spec) if m is not None: # Raises ValueError @@ -518,8 +518,8 @@ class Period: self.prep_desc = gettext("In %s") % self.description return # A specific date period - m = re.match(("^([0-9]{4})-([0-9]{2})-([0-9]{2})" - "-([0-9]{4})-([0-9]{2})-([0-9]{2})$"), + m = re.match((r"^(\d{4})-(\d{2})-(\d{2})" + r"-(\d{4})-(\d{2})-(\d{2})$"), spec) if m is not None: # Raises ValueError @@ -564,7 +564,7 @@ class Period: self.prep_desc = gettext("In %s") % self.description return # Until a specific day - m = re.match("^-([0-9]{4})-([0-9]{2})-([0-9]{2})$", spec) + m = re.match(r"^-(\d{4})-(\d{2})-(\d{2})$", spec) if m is not None: # Raises ValueError self.end = datetime.date(