Skip to content

Commit 19cf38a

Browse files
Add Emails class for email validation and uniqueness
1 parent 71f5b39 commit 19cf38a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Week03/emails_aysegul_yildiz.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import re
2+
3+
4+
class Emails(list):
5+
def __init__(self, emails):
6+
self.validate(emails)
7+
8+
unique_emails = list(set(emails))
9+
super().__init__(unique_emails)
10+
self.data = unique_emails
11+
12+
def validate(self, emails):
13+
if not isinstance(emails, list):
14+
raise ValueError
15+
16+
email_regex = re.compile(
17+
r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
18+
)
19+
20+
for email in emails:
21+
if not isinstance(email, str):
22+
raise ValueError
23+
if not email_regex.match(email):
24+
raise ValueError
25+
26+
def __repr__(self):
27+
return f"Emails({list(self)})"
28+
29+
def __str__(self):
30+
return "\n".join(self)

0 commit comments

Comments
 (0)