Skip to content

Commit bed099d

Browse files
authored
Create Emails class with validation and uniqueness
Implement Emails class for storing and validating email addresses.
1 parent 71f5b39 commit bed099d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Week05/emails_gamze_kilinc.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import re
2+
3+
4+
class Emails(list):
5+
"""
6+
A list subclass that stores and validates email addresses.
7+
"""
8+
9+
def __init__(self, data):
10+
self.validate(data)
11+
# remove duplicates
12+
unique_emails = list(set(data))
13+
super().__init__(unique_emails)
14+
self.data = self # testlerde .data kullanıldığı için
15+
16+
def validate(self, data):
17+
# check all items are strings
18+
for item in data:
19+
if not isinstance(item, str):
20+
raise ValueError("All items must be strings")
21+
22+
# email regex
23+
email_pattern = re.compile(r"^[^@]+@[^@]+\.[^@]+$")
24+
25+
for email in data:
26+
if not email_pattern.match(email):
27+
raise ValueError("Invalid email address")
28+
29+
def __repr__(self):
30+
return f"{self.__class__.__name__}({list(self)})"
31+
32+
def __str__(self):
33+
return ", ".join(self)

0 commit comments

Comments
 (0)