Skip to content

Commit 09f7af2

Browse files
Implement Emails class for email validation
1 parent 8cfff6e commit 09f7af2

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

‎Week05/emails_beytullah_arslan.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Emails(list):
2+
def __init__(self, emails):
3+
validated = self.validate(emails)
4+
super().__init__(validated)
5+
self.data = validated
6+
7+
def validate(self, emails):
8+
unique = []
9+
for email in emails:
10+
if not isinstance(email, str):
11+
raise ValueError(f"Email must be a string: {email!r}")
12+
if email.count("@") != 1:
13+
raise ValueError(f"Invalid email address: {email}")
14+
user, domain = email.split("@")
15+
if user == "" or "." not in domain:
16+
raise ValueError(f"Invalid email address: {email}")
17+
if domain.startswith(".") or domain.endswith("."):
18+
raise ValueError(f"Invalid email address: {email}")
19+
if email not in unique:
20+
unique.append(email)
21+
return unique
22+
23+
def __repr__(self):
24+
return f"Emails({self.data!r})"
25+
26+
def __str__(self):
27+
return ", ".join(self.data)

0 commit comments

Comments
 (0)