Skip to content

Commit d7b1ef8

Browse files
authored
Implement Emails class for unique email validation
1 parent 71f5b39 commit d7b1ef8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Emails(list):
2+
def __init__(self, emails):
3+
unique_emails = []
4+
seen = set()
5+
for email in emails:
6+
if email not in seen:
7+
unique_emails.append(email)
8+
seen.add(email)
9+
10+
self.validate(unique_emails)
11+
super().__init__(unique_emails)
12+
self.data = self
13+
14+
def validate(self, emails):
15+
for email in emails:
16+
if not isinstance(email, str):
17+
raise ValueError("All items must be strings")
18+
19+
if "@" not in email:
20+
raise ValueError("Email must contain @")
21+
22+
parts = email.split("@")
23+
if len(parts) != 2 or "." not in parts[1]:
24+
raise ValueError("Invalid domain format")
25+
26+
def __repr__(self):
27+
return f"Emails({super().__repr__()})"
28+
29+
def __str__(self):
30+
return super().__str__()

0 commit comments

Comments
 (0)