Skip to content

Commit d4e93ac

Browse files
authored
Add Emails class for email validation and storage
1 parent 8cfff6e commit d4e93ac

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎Week05/emails_batuhan_tunc.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import re
2+
3+
4+
class Emails(list):
5+
EMAIL_PATTERN = re.compile(r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}$")
6+
7+
def __init__(self, emails):
8+
super().__init__()
9+
self.data = self.validate(emails)
10+
self.extend(self.data)
11+
12+
def validate(self, emails):
13+
for email in emails:
14+
if not isinstance(email, str):
15+
raise ValueError("All items must be strings")
16+
if not self.EMAIL_PATTERN.match(email):
17+
raise ValueError(f"Invalid email address: {email}")
18+
return list(dict.fromkeys(emails))
19+
20+
def __repr__(self):
21+
return f"{self.__class__.__name__}({self.data!r})"
22+
23+
def __str__(self):
24+
return ", ".join(self.data)

0 commit comments

Comments
 (0)