Skip to content

Commit ea49dcb

Browse files
committed
Add Week05 homework
1 parent 8cfff6e commit ea49dcb

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

‎Week05/emails_berkay_altunbag.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import re
2+
3+
4+
class Emails(list):
5+
"""A list of unique, validated email addresses."""
6+
7+
_pattern = re.compile(r"^[\w.+-]+@[\w-]+(\.[\w-]+)+$")
8+
9+
def __init__(self, emails):
10+
self.validate(emails)
11+
self.data = list(dict.fromkeys(emails))
12+
super().__init__(self.data)
13+
14+
def validate(self, emails):
15+
for email in emails:
16+
if not isinstance(email, str):
17+
raise ValueError(f"{email!r} is not a string")
18+
if not self._pattern.match(email):
19+
raise ValueError(f"{email!r} is not a valid email address")
20+
21+
def __repr__(self):
22+
return f"Emails({self.data!r})"
23+
24+
def __str__(self):
25+
return ", ".join(self.data)

0 commit comments

Comments
 (0)