From 886c5e2ae78f6e9fd78508b4e70439a39ae818e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sefa=20Sa=C4=9F=C4=B1rl=C4=B1?= Date: Sat, 7 Nov 2026 00:56:48 +0300 Subject: [PATCH] =?UTF-8?q?Week05:=20emails=20=C3=B6devi=20eklendi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week05/emails_sefa_sagirli.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Week05/emails_sefa_sagirli.py diff --git a/Week05/emails_sefa_sagirli.py b/Week05/emails_sefa_sagirli.py new file mode 100644 index 00000000..81e15b33 --- /dev/null +++ b/Week05/emails_sefa_sagirli.py @@ -0,0 +1,35 @@ +import re + + +class Emails(list): + """A list of unique and valid email addresses.""" + + EMAIL_PATTERN = re.compile(r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}$") + + def __init__(self, emails): + self.data = self.validate(emails) + super().__init__(self.data) + + def validate(self, emails): + """ + Check the given email addresses and remove the duplicates. + + :param emails: The email addresses to validate + :raises ValueError: If an item is not a string or not a valid email address + :return: A list of unique email addresses in their original order + """ + unique_emails = [] + for email in emails: + if not isinstance(email, str): + raise ValueError(f"{email!r} is not a string") + if not self.EMAIL_PATTERN.match(email): + raise ValueError(f"{email!r} is not a valid email address") + if email not in unique_emails: + unique_emails.append(email) + return unique_emails + + def __repr__(self): + return f"{self.__class__.__name__}({self.data!r})" + + def __str__(self): + return ", ".join(self.data)