Skip to content

Commit bacffdf

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

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Week05/emails_dilara_agac.py

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

0 commit comments

Comments
 (0)