From 93966e84562516d4930c09bace85554f9ce760a2 Mon Sep 17 00:00:00 2001 From: sinemtanriver Date: Wed, 28 Oct 2026 14:40:26 +0300 Subject: [PATCH] Add Emails class for email validation and representation --- Week05/emails_sinem_tanriver.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Week05/emails_sinem_tanriver.py diff --git a/Week05/emails_sinem_tanriver.py b/Week05/emails_sinem_tanriver.py new file mode 100644 index 000000000..37bbcc493 --- /dev/null +++ b/Week05/emails_sinem_tanriver.py @@ -0,0 +1,30 @@ +class Emails(list): + def __init__(self, email_list): + checked = self.validate(email_list) + super().__init__(checked) + self.data = checked + + def validate(self, email_list): + result = [] + for item in email_list: + if type(item) is not str: + raise ValueError("Only strings are allowed") + if item.count("@") != 1: + raise ValueError("Invalid email: " + item) + user, domain = item.split("@") + if user == "" or "." not in domain: + raise ValueError("Invalid email: " + item) + if domain.startswith(".") or domain.endswith("."): + raise ValueError("Invalid email: " + item) + if item not in result: + result.append(item) + return result + + def __repr__(self): + return "Emails(" + str(list(self)) + ")" + + def __str__(self): + text = "Emails:\n" + for i, mail in enumerate(self, start=1): + text += f"{i}. {mail}\n" + return text