Skip to content

Commit 14459f8

Browse files
committed
Optimize usage of re. methods
1 parent d1a874d commit 14459f8

File tree

4 files changed

+6
-8
lines changed

4 files changed

+6
-8
lines changed

pendulum/formatting/formatter.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ def parse(
376376
"""
377377
escaped_fmt = re.escape(fmt)
378378

379-
tokens = self._FROM_FORMAT_RE.findall(escaped_fmt)
380-
if not tokens:
379+
if not self._FROM_FORMAT_RE.search(escaped_fmt):
381380
raise ValueError("The given time string does not match the given format")
382381

383382
if not locale:
@@ -405,7 +404,7 @@ def parse(
405404
lambda m: self._replace_tokens(m.group(0), loaded_locale), escaped_fmt
406405
)
407406

408-
if not re.search("^" + pattern + "$", time):
407+
if not re.fullmatch(pattern, time):
409408
raise ValueError(f"String does not match format {fmt}")
410409

411410
def _get_parsed_values(m: Match[str]) -> Any:
@@ -629,7 +628,6 @@ def _get_parsed_locale_value(
629628
match = "months.abbreviated"
630629
elif token == "Do":
631630
parsed["day"] = int(cast(Match[str], re.match(r"(\d+)", value)).group(1))
632-
633631
return
634632
elif token == "dddd":
635633
unit = "day_of_week"

pendulum/locales/locale.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def load(cls, locale: str | Locale) -> Locale:
4848

4949
@classmethod
5050
def normalize_locale(cls, locale: str) -> str:
51-
m = re.match("([a-z]{2})[-_]([a-z]{2})", locale, re.I)
51+
m = re.fullmatch("([a-z]{2})[-_]([a-z]{2})", locale, re.I)
5252
if m:
5353
return f"{m.group(1).lower()}_{m.group(2).lower()}"
5454
else:

pendulum/parsing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def _parse_common(text: str, **options: Any) -> datetime | date | time:
140140
141141
:param text: The string to parse.
142142
"""
143-
m = COMMON.match(text)
143+
m = COMMON.fullmatch(text)
144144
has_date = False
145145
year = 0
146146
month = 1

pendulum/parsing/iso8601.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def parse_iso8601(
9696
if parsed is not None:
9797
return parsed
9898

99-
m = ISO8601_DT.match(text)
99+
m = ISO8601_DT.fullmatch(text)
100100
if not m:
101101
raise ParserError("Invalid ISO 8601 string")
102102

@@ -263,7 +263,7 @@ def parse_iso8601(
263263

264264

265265
def _parse_iso8601_duration(text: str, **options: str) -> Duration | None:
266-
m = ISO8601_DURATION.match(text)
266+
m = ISO8601_DURATION.fullmatch(text)
267267
if not m:
268268
return None
269269

0 commit comments

Comments
 (0)