Skip to content

Commit 5462f39

Browse files
authored
Add Emails class for email validation and storage
Implements an Emails class for validating and storing unique email addresses.
1 parent 71f5b39 commit 5462f39

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Week05/emails_almira_keles.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from __future__ import annotations
2+
3+
import re
4+
from typing import Iterable, List
5+
6+
7+
_EMAIL_RE = re.compile(
8+
# Basit ama yeterli doğrulama:
9+
# local@domain.tld (en az bir nokta içeren domain)
10+
r"^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$"
11+
)
12+
13+
14+
class Emails(list):
15+
"""
16+
A list-like container that stores validated, unique email addresses.
17+
- Accepts an iterable of emails on init.
18+
- Validates: items must be strings and must look like valid emails.
19+
- Removes duplicates (keeps first occurrence).
20+
- Stores the final list in self.data as well.
21+
"""
22+
23+
def __init__(self, emails: Iterable[str]):
24+
# list subclass init
25+
super().__init__()
26+
self.data: List[str] = self.validate(emails)
27+
# keep list content consistent with .data
28+
self.extend(self.data)
29+
30+
@staticmethod
31+
def validate(emails: Iterable[str]) -> List[str]:
32+
# Iterable kontrolü: testler liste gönderiyor ama genel olsun.
33+
if emails is None:
34+
raise ValueError("Emails cannot be None")
35+
36+
unique: List[str] = []
37+
seen = set()
38+
39+
for item in emails:
40+
if not isinstance(item, str):
41+
raise ValueError("All email addresses must be strings")
42+
43+
email = item.strip()
44+
if not _EMAIL_RE.match(email):
45+
raise ValueError(f"Invalid email address: {item}")
46+
47+
# duplicate engelle (case-sensitive bırakıyorum; test bunu zorlamıyor)
48+
if email not in seen:
49+
seen.add(email)
50+
unique.append(email)
51+
52+
return unique
53+
54+
def __repr__(self) -> str:
55+
# Testin beklediği kritik nokta:
56+
# repr(obj) == obj.__repr__()
57+
# ayrıca "reproduce" teması: Emails([...]) biçiminde üretilebilir.
58+
return f"Emails({self.data!r})"
59+
60+
def __str__(self) -> str:
61+
# İnsan okunur: virgülle birleştir
62+
return ", ".join(self.data)
63+
Footer

0 commit comments

Comments
 (0)