Skip to content

Commit 2cfc1af

Browse files
committed
Add Week05 Emails class
1 parent 9a399e7 commit 2cfc1af

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

‎Week05/emails_beyza_arman.py‎

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

0 commit comments

Comments
 (0)