Skip to content

Commit 8b692af

Browse files
authored
Merge pull request #94 from usrmertc/patch-4
Create emails_mert_can_fidan.py
2 parents 332028c + 011f929 commit 8b692af

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Week05/emails_mert_can_fidan.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import re
2+
3+
class Emails(list):
4+
def __init__(self, data: [str]) -> None:
5+
self.data = list(set(data))
6+
self.validate(data)
7+
8+
def __repr__(self):
9+
return f"Emails({self.data})"
10+
11+
def __str__(self):
12+
return f"Emails: {self.data}"
13+
14+
def validate(self, data: [str]) -> None:
15+
# Validate email addresses according to the pattern used by Gmail.
16+
# The local part (before the @) can contain:
17+
# - Alphanumeric characters
18+
# - Periods (.) but not consecutively or at the start/end
19+
# The domain part (after the @) can contain:
20+
# - Alphanumeric characters
21+
# - Periods (.) followed by at least two alphabetic characters
22+
# This pattern also supports subdomains.
23+
regex_pattern = r"^[A-Za-z0-9]+(?:[.][A-Za-z0-9]+)*@[A-Za-z0-9]+(?:\.[A-Za-z]{2,}){1,2}$"
24+
25+
for email in data:
26+
if isinstance(email, int):
27+
raise ValueError("Only string values accepted!")
28+
if not re.match(regex_pattern, email):
29+
raise ValueError(f"Invalid email format: {email}")

0 commit comments

Comments
 (0)