-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_review.py
57 lines (46 loc) · 1.42 KB
/
code_review.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Email Validator Utility
This module provides an EmailValidator class for validating and normalizing email addresses.
Usage:
validator = EmailValidator()
if validator.validate(email):
normalized_email = validator.normalize(email)
"""
import re
class EmailValidator:
def validate(self, email):
"""Validates an email address."""
if not isinstance(email, str):
raise ValueError("Email must be a string")
if "@" not in email:
return False
localpart, domain = email.split("@")
if len(localpart) < 1 or len(localpart) > 64:
return False
return True
def normalize(self, email):
"""Normalizes an email address."""
# remove leading/trailing whitespace
email = email.strip()
# remove dots from localpart
localpart, domain = email.split("@")
localpart.replace(".", "")
# convert domain to lowercase
normalized = f"{localpart}@{domain.lower()}"
return normalized
if __name__ == "__main__":
validator = EmailValidator()
emails = [
"alice@example.com",
"bob.smith@example.com",
"invalid.email",
"toolonglocalpartxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@example.com",
" carol@example.com "
]
for email in emails:
if validator.validate(email):
print(f"Valid: {email}")
normalized = validator.normalize(email)
print(f"Normalized: {normalized}")
else:
print(f"Invalid: {email}")