Skip to content

Commit 50e4cdd

Browse files
authored
Create emails_pelin_akkus.py
1 parent d1f313f commit 50e4cdd

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎Week05/emails_pelin_akkus.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import re
2+
3+
4+
class Emails(list):
5+
"""A list that stores unique, valid email addresses."""
6+
7+
def __init__(self, emails=None):
8+
super().__init__()
9+
self.data = self
10+
11+
if emails is None:
12+
emails = []
13+
14+
for email in emails:
15+
self.append(email)
16+
17+
@staticmethod
18+
def validate(email):
19+
"""Check an email address and raise ValueError if it is invalid."""
20+
if not isinstance(email, str):
21+
raise ValueError("Email must be a string")
22+
23+
# Simple format check for [email protected]
24+
pattern = r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+$"
25+
if not re.fullmatch(pattern, email):
26+
raise ValueError("Invalid email address")
27+
return True
28+
29+
def append(self, email):
30+
self.validate(email)
31+
# Keep only one copy of each address.
32+
if email not in self:
33+
super().append(email)
34+
35+
def __str__(self):
36+
return ", ".join(self)
37+
38+
def __repr__(self):
39+
return f"Emails({list(self)!r})"

0 commit comments

Comments
 (0)