Skip to content

Commit 080095a

Browse files
Create Emails class for email validation and uniqueness
Implement Emails class to manage unique email addresses with validation.
1 parent 71f5b39 commit 080095a

File tree

1 file changed

+29
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)