From bed099db70a04ff8b8351bda2b4c669460d90845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gamze=20K=C4=B1l=C4=B1n=C3=A7?= <149265010+gamzeklnc@users.noreply.github.com> Date: Mon, 5 Jan 2026 13:47:35 +0300 Subject: [PATCH] Create Emails class with validation and uniqueness Implement Emails class for storing and validating email addresses. --- Week05/emails_gamze_kilinc.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Week05/emails_gamze_kilinc.py diff --git a/Week05/emails_gamze_kilinc.py b/Week05/emails_gamze_kilinc.py new file mode 100644 index 00000000..08c89959 --- /dev/null +++ b/Week05/emails_gamze_kilinc.py @@ -0,0 +1,33 @@ +import re + + +class Emails(list): + """ + A list subclass that stores and validates email addresses. + """ + + def __init__(self, data): + self.validate(data) + # remove duplicates + unique_emails = list(set(data)) + super().__init__(unique_emails) + self.data = self # testlerde .data kullanıldığı için + + def validate(self, data): + # check all items are strings + for item in data: + if not isinstance(item, str): + raise ValueError("All items must be strings") + + # email regex + email_pattern = re.compile(r"^[^@]+@[^@]+\.[^@]+$") + + for email in data: + if not email_pattern.match(email): + raise ValueError("Invalid email address") + + def __repr__(self): + return f"{self.__class__.__name__}({list(self)})" + + def __str__(self): + return ", ".join(self)