Skip to content

Commit b92af5b

Browse files
authored
Add files via upload
1 parent 6fd68cf commit b92af5b

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎Week05/emails_yagiz_cengiz.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import re
2+
3+
4+
class Emails(list):
5+
"""
6+
This class keeps a list of unique and valid email addresses.
7+
"""
8+
9+
def __init__(self, emails: list) -> None:
10+
super().__init__(self.validate(emails))
11+
12+
@property
13+
def data(self) -> "Emails":
14+
"""
15+
This property returns the email addresses.
16+
"""
17+
return self
18+
19+
@staticmethod
20+
def validate(emails: list) -> list:
21+
"""
22+
This method checks the email addresses and removes the duplicates.
23+
"""
24+
for email in emails:
25+
if not isinstance(email, str):
26+
raise ValueError(f"{email!r} is not a string")
27+
if not re.fullmatch(r"[\w.+-]+@[\w-]+(\.[\w-]+)+", email):
28+
raise ValueError(f"{email!r} is not a valid email address")
29+
return list(dict.fromkeys(emails))
30+
31+
def __repr__(self) -> str:
32+
return f"Emails({list(self)!r})"
33+
34+
def __str__(self) -> str:
35+
return ", ".join(self)

0 commit comments

Comments
 (0)