Skip to content

Commit 36a4f2b

Browse files
Create Emails class for email validation and storage
Implement Emails class to validate and store unique email addresses.
1 parent 3806f1e commit 36a4f2b

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import re
2+
3+
4+
class Emails(list):
5+
6+
def __init__(self, emails):
7+
super().__init__()
8+
self.data = []
9+
10+
for email in emails:
11+
self.validate(email)
12+
13+
if email not in self.data:
14+
self.data.append(email)
15+
super().append(email)
16+
17+
def validate(self, email):
18+
if not isinstance(email, str):
19+
raise ValueError("Email must be a string")
20+
21+
pattern = r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$"
22+
23+
if not re.fullmatch(pattern, email):
24+
raise ValueError("Invalid email address")
25+
26+
return True
27+
28+
def __repr__(self):
29+
return f"Emails({self.data!r})"
30+
31+
def __str__(self):
32+
return str(self.data)

0 commit comments

Comments
 (0)