Skip to content

Commit

Permalink
Optimize usage of re. methods
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro committed Aug 19, 2023
1 parent d1a874d commit 14459f8
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
6 changes: 2 additions & 4 deletions pendulum/formatting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ def parse(
"""
escaped_fmt = re.escape(fmt)

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

if not locale:
Expand Down Expand Up @@ -405,7 +404,7 @@ def parse(
lambda m: self._replace_tokens(m.group(0), loaded_locale), escaped_fmt
)

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

def _get_parsed_values(m: Match[str]) -> Any:
Expand Down Expand Up @@ -629,7 +628,6 @@ def _get_parsed_locale_value(
match = "months.abbreviated"
elif token == "Do":
parsed["day"] = int(cast(Match[str], re.match(r"(\d+)", value)).group(1))

return
elif token == "dddd":
unit = "day_of_week"
Expand Down
2 changes: 1 addition & 1 deletion pendulum/locales/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def load(cls, locale: str | Locale) -> Locale:

@classmethod
def normalize_locale(cls, locale: str) -> str:
m = re.match("([a-z]{2})[-_]([a-z]{2})", locale, re.I)
m = re.fullmatch("([a-z]{2})[-_]([a-z]{2})", locale, re.I)
if m:
return f"{m.group(1).lower()}_{m.group(2).lower()}"
else:
Expand Down
2 changes: 1 addition & 1 deletion pendulum/parsing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _parse_common(text: str, **options: Any) -> datetime | date | time:
:param text: The string to parse.
"""
m = COMMON.match(text)
m = COMMON.fullmatch(text)
has_date = False
year = 0
month = 1
Expand Down
4 changes: 2 additions & 2 deletions pendulum/parsing/iso8601.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def parse_iso8601(
if parsed is not None:
return parsed

m = ISO8601_DT.match(text)
m = ISO8601_DT.fullmatch(text)
if not m:
raise ParserError("Invalid ISO 8601 string")

Expand Down Expand Up @@ -263,7 +263,7 @@ def parse_iso8601(


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

Expand Down

0 comments on commit 14459f8

Please sign in to comment.