Compare commits

..

5 Commits

Author SHA1 Message Date
CaedenPH
60ca06bfee Merge branch 'create-is-valid-email' of https://github.com/caedenph/python into create-is-valid-email 2023-08-10 15:17:01 +01:00
CaedenPH
558aa54560 chore(is_valid_email_address): Fix ruff error 2023-08-10 15:16:50 +01:00
pre-commit-ci[bot]
bfd99cbc49 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-08-10 14:14:56 +00:00
Caeden Perelli-Harris
09fc6e8cb3
Update strings/is_valid_email_address.py
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-08-10 15:14:21 +01:00
CaedenPH
15c5482931 chore(is_valid_email_address): Implement changes from code review 2023-08-10 15:13:36 +01:00

View File

@ -4,7 +4,6 @@ Implements an is valid email address algorithm
@ https://en.wikipedia.org/wiki/Email_address
"""
import re
import string
email_tests: tuple[tuple[str, bool], ...] = (
@ -32,6 +31,7 @@ email_tests: tuple[tuple[str, bool], ...] = (
False,
),
("i.like.underscores@but_its_not_allowed_in_this_part", False),
("", False),
)
# The maximum octets (one character as a standard unicode character is one byte)
@ -69,7 +69,7 @@ def is_valid_email_address(email: str) -> bool:
last character, and may not have more than one "." consecutively.
>>> for email, valid in email_tests:
... assert is_valid_email_address(email) is valid
... assert is_valid_email_address(email) == valid
"""
# (1.) Make sure that there is only one @ symbol in the email address
@ -88,12 +88,8 @@ def is_valid_email_address(email: str) -> bool:
):
return False
# (4.) Validate the placement of "." characters
if (
local_part.startswith(".")
or local_part.endswith(".")
or re.search(r"\.\.+", local_part)
):
# (4.) Validate the placement of "." characters in the local-part
if local_part.startswith(".") or local_part.endswith(".") or ".." in local_part:
return False
# (5.) Validate the characters in the domain
@ -105,7 +101,7 @@ def is_valid_email_address(email: str) -> bool:
return False
# (7.) Validate the placement of "." characters
if domain.startswith(".") or domain.endswith(".") or re.search(r"\.\.+", domain):
if domain.startswith(".") or domain.endswith(".") or ".." in domain:
return False
return True
@ -118,4 +114,4 @@ if __name__ == "__main__":
for email, valid in email_tests:
is_valid = is_valid_email_address(email)
assert is_valid == valid, f"{email} is {is_valid}"
print(f"Email address {email} is {'not ' if is_valid is False else ''}valid")
print(f"Email address {email} is {'not ' if is_valid else ''}valid")