mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 10:28:39 +00:00
Compare commits
5 Commits
b94f13cba8
...
60ca06bfee
Author | SHA1 | Date | |
---|---|---|---|
|
60ca06bfee | ||
|
558aa54560 | ||
|
bfd99cbc49 | ||
|
09fc6e8cb3 | ||
|
15c5482931 |
@ -4,7 +4,6 @@ Implements an is valid email address algorithm
|
|||||||
@ https://en.wikipedia.org/wiki/Email_address
|
@ https://en.wikipedia.org/wiki/Email_address
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
|
||||||
import string
|
import string
|
||||||
|
|
||||||
email_tests: tuple[tuple[str, bool], ...] = (
|
email_tests: tuple[tuple[str, bool], ...] = (
|
||||||
@ -32,6 +31,7 @@ email_tests: tuple[tuple[str, bool], ...] = (
|
|||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
("i.like.underscores@but_its_not_allowed_in_this_part", 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)
|
# 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.
|
last character, and may not have more than one "." consecutively.
|
||||||
|
|
||||||
>>> for email, valid in email_tests:
|
>>> 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
|
# (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
|
return False
|
||||||
|
|
||||||
# (4.) Validate the placement of "." characters
|
# (4.) Validate the placement of "." characters in the local-part
|
||||||
if (
|
if local_part.startswith(".") or local_part.endswith(".") or ".." in local_part:
|
||||||
local_part.startswith(".")
|
|
||||||
or local_part.endswith(".")
|
|
||||||
or re.search(r"\.\.+", local_part)
|
|
||||||
):
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# (5.) Validate the characters in the domain
|
# (5.) Validate the characters in the domain
|
||||||
@ -105,7 +101,7 @@ def is_valid_email_address(email: str) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
# (7.) Validate the placement of "." characters
|
# (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 False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -118,4 +114,4 @@ if __name__ == "__main__":
|
|||||||
for email, valid in email_tests:
|
for email, valid in email_tests:
|
||||||
is_valid = is_valid_email_address(email)
|
is_valid = is_valid_email_address(email)
|
||||||
assert is_valid == valid, f"{email} is {is_valid}"
|
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")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user