Skip to content

Commit 93966e8

Browse files
Add Emails class for email validation and representation
1 parent f083b29 commit 93966e8

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎Week05/emails_sinem_tanriver.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Emails(list):
2+
def __init__(self, email_list):
3+
checked = self.validate(email_list)
4+
super().__init__(checked)
5+
self.data = checked
6+
7+
def validate(self, email_list):
8+
result = []
9+
for item in email_list:
10+
if type(item) is not str:
11+
raise ValueError("Only strings are allowed")
12+
if item.count("@") != 1:
13+
raise ValueError("Invalid email: " + item)
14+
user, domain = item.split("@")
15+
if user == "" or "." not in domain:
16+
raise ValueError("Invalid email: " + item)
17+
if domain.startswith(".") or domain.endswith("."):
18+
raise ValueError("Invalid email: " + item)
19+
if item not in result:
20+
result.append(item)
21+
return result
22+
23+
def __repr__(self):
24+
return "Emails(" + str(list(self)) + ")"
25+
26+
def __str__(self):
27+
text = "Emails:\n"
28+
for i, mail in enumerate(self, start=1):
29+
text += f"{i}. {mail}\n"
30+
return text

0 commit comments

Comments
 (0)