Skip to content

Commit 5f4f4bc

Browse files
authored
Create email_burakcan_aksoy.py
1 parent 71f5b39 commit 5f4f4bc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Week05/email_burakcan_aksoy.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import re
2+
3+
class Emails(list):
4+
EMAIL_REGEX = re.compile(r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$")
5+
6+
def __init__(self, items):
7+
self._check(items)
8+
cleaned = self._unique(items)
9+
super().__init__(cleaned)
10+
self.data = cleaned
11+
12+
def _check(self, items):
13+
if not all(type(x) is str for x in items):
14+
raise ValueError("All items must be strings")
15+
16+
for mail in items:
17+
if not self.EMAIL_REGEX.fullmatch(mail):
18+
raise ValueError("Invalid email address")
19+
20+
@staticmethod
21+
def _unique(items):
22+
seen = []
23+
for x in items:
24+
if x not in seen:
25+
seen.append(x)
26+
return seen
27+
28+
def __repr__(self):
29+
return f"{self.__class__.__name__}({list(self)})"
30+
31+
def __str__(self):
32+
return "\n".join(self)

0 commit comments

Comments
 (0)