Skip to content

Commit 1f5ac8d

Browse files
Create emails_hatice_akgul.py
1 parent 71f5b39 commit 1f5ac8d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Week05/emails_hatice_akgul.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import re
2+
3+
class Emails(list):
4+
"""
5+
Emails class that extends list and validates email addresses.
6+
"""
7+
def __init__(self, email_list):
8+
validated_data = self.validate(email_list)
9+
super().__init__(validated_data)
10+
self.data = list(validated_data)
11+
12+
@staticmethod
13+
def validate(email_list):
14+
"""
15+
Validates that all items are strings and follow email format.
16+
Removes duplicates while preserving order (or just unique set).
17+
"""
18+
seen = []
19+
email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
20+
21+
for email in email_list:
22+
if not isinstance(email, str):
23+
raise ValueError("All items must be strings.")
24+
25+
if not re.match(email_regex, email):
26+
raise ValueError(f"Invalid email format: {email}")
27+
28+
if email not in seen:
29+
seen.append(email)
30+
31+
return seen
32+
33+
def __repr__(self):
34+
"""
35+
Returns a string representation that can recreate the object.
36+
"""
37+
return f"Emails({list(self)})"
38+
39+
def __str__(self):
40+
"""
41+
Returns a user-friendly string representation.
42+
"""
43+
return f"Email List: {list(self)}"

0 commit comments

Comments
 (0)