Skip to content

Commit ffe902a

Browse files
authored
Add Emails class for email validation and storage
1 parent 71f5b39 commit ffe902a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Week05/emails_mehmet_usta.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import re
2+
3+
class Emails(list):
4+
def __init__(self, data):
5+
self.validate(data)
6+
unique_emails = list(set(data))
7+
super().__init__(unique_emails)
8+
self.data = unique_emails
9+
10+
def validate(self, data):
11+
if not all(isinstance(x, str) for x in data):
12+
raise ValueError("All items must be strings")
13+
14+
pattern = re.compile(r"^[\w\.-]+@[\w\.-]+\.\w+$")
15+
16+
for email in data:
17+
if not pattern.match(email):
18+
raise ValueError("Invalid email address")
19+
20+
def __repr__(self):
21+
return f"Emails({list(self)})"
22+
23+
def __str__(self):
24+
return "\n".join(self)

0 commit comments

Comments
 (0)