Skip to content

Commit cdb3634

Browse files
authored
Merge pull request #983 from gktgkayac/patch-7
Add Emails class for email validation and storage
2 parents 8328641 + 530cd97 commit cdb3634

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎Week05/emails_goktug_kayaci.py‎

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+
self.data = []
8+
9+
for email in emails:
10+
self.validate(email)
11+
12+
if email not in self.data:
13+
self.data.append(email)
14+
15+
super().__init__(self.data)
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 ", ".join(self.data)

0 commit comments

Comments
 (0)