Skip to content

Commit 9a4312f

Browse files
authored
Add Emails class for email validation and storage
1 parent 2024d08 commit 9a4312f

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎Week05/emails_mehmet_balci.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+
6+
EMAIL_PATTERN = re.compile(r"[^@\s]+@[^@\s]+\.[^@\s]+")
7+
8+
def __init__(self, emails):
9+
super().__init__()
10+
self.validate(emails)
11+
for email in emails:
12+
if email not in self: # tekrar edenleri ekleme
13+
self.append(email)
14+
15+
@property
16+
def data(self):
17+
return list(self)
18+
19+
def validate(self, emails):
20+
for email in emails:
21+
if not isinstance(email, str):
22+
raise ValueError(f"{email!r} is not a string")
23+
if not self.EMAIL_PATTERN.fullmatch(email):
24+
raise ValueError(f"{email!r} is not a valid email address")
25+
26+
def __repr__(self):
27+
return f"Emails({list.__repr__(self)})"
28+
29+
def __str__(self):
30+
return ", ".join(self)

0 commit comments

Comments
 (0)