Skip to content

Commit 8f15d0c

Browse files
authored
Create emails_Sevval_Ok.py
1 parent 71f5b39 commit 8f15d0c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Week05/emails_Sevval_Ok.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import re
2+
from typing import Iterable, List
3+
4+
class Emails(list)
5+
_EMAIL_RE = re.compile(
6+
r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$"
7+
)
8+
9+
@classmethod
10+
def validate(cls, items: Iterable) -> List[str]:
11+
cleaned = []
12+
for it in items:
13+
if not isinstance(it, str):
14+
raise ValueError
15+
if not cls._EMAIL_RE.match(it):
16+
raise ValueError
17+
cleaned.append(it)
18+
return cleaned
19+
20+
def __init__(self, items):
21+
validated = self.validate(items)
22+
23+
unique = list(dict.fromkeys(validated))
24+
25+
self.data = unique # tests check .data.count(...)
26+
super().__init__(unique)
27+
28+
def __repr__(self) -> str:
29+
return f"{self.__class__.__name__}({self.data!r})"
30+
31+
def __str__(self) -> str:
32+
return ", ".join(self.data)

0 commit comments

Comments
 (0)