Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Optimize usage of re.* methods #741

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading