Skip to content

Commit c273f77

Browse files
authored
Merge pull request canbula#768 from meldag006/patch-6
Create Emails class for email validation and storage
2 parents 64bad12 + 1a71828 commit c273f77

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

‎Week05/emails_melda_gungor.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Emails(list):
2+
def __init__(self, emails=None):
3+
if emails is None:
4+
emails = []
5+
6+
valid_unique_emails = []
7+
for email in emails:
8+
self.validate(email)
9+
if email not in valid_unique_emails:
10+
valid_unique_emails.append(email)
11+
12+
13+
super().__init__(valid_unique_emails)
14+
self.data = self
15+
16+
@staticmethod
17+
def validate(email):
18+
if not isinstance(email, str):
19+
raise ValueError(f"email must be a string, got {type(email)}")
20+
if email.count("@") != 1:
21+
raise ValueError(f"invalid email address: {email}")
22+
23+
user, domain = email.split("@")
24+
25+
if not user or not domain:
26+
raise ValueError(f"invalid email address: {email}")
27+
28+
if "." not in domain or domain.startswith(".") or domain.endswith("."):
29+
raise ValueError(f"invalid email address: {email}")
30+
31+
return True
32+
33+
def __repr__(self):
34+
return f"Emails({list(self)})"
35+
36+
def __str__(self):
37+
return super().__str__()
38+

0 commit comments

Comments
 (0)