Skip to content

Commit 71e37f6

Browse files
authored
Optimize usage of re. methods (#741)
1 parent 91d0c1e commit 71e37f6

File tree

4 files changed

+6
-7
lines changed

4 files changed

+6
-7
lines changed

src/pendulum/formatting/formatter.py

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

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

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

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

412411
def _get_parsed_values(m: Match[str]) -> Any:

src/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:

src/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

src/pendulum/parsing/iso8601.py

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

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

@@ -264,7 +264,7 @@ def parse_iso8601(
264264

265265

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

0 commit comments

Comments
 (0)