From ffe902a6efe4425209bf92963ecd1c1549800060 Mon Sep 17 00:00:00 2001 From: mehmetusta03 Date: Tue, 23 Dec 2025 12:59:01 +0300 Subject: [PATCH] Add Emails class for email validation and storage --- Week05/emails_mehmet_usta.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Week05/emails_mehmet_usta.py diff --git a/Week05/emails_mehmet_usta.py b/Week05/emails_mehmet_usta.py new file mode 100644 index 00000000..f355dde9 --- /dev/null +++ b/Week05/emails_mehmet_usta.py @@ -0,0 +1,24 @@ +import re + +class Emails(list): + def __init__(self, data): + self.validate(data) + unique_emails = list(set(data)) + super().__init__(unique_emails) + self.data = unique_emails + + def validate(self, data): + if not all(isinstance(x, str) for x in data): + raise ValueError("All items must be strings") + + pattern = re.compile(r"^[\w\.-]+@[\w\.-]+\.\w+$") + + for email in data: + if not pattern.match(email): + raise ValueError("Invalid email address") + + def __repr__(self): + return f"Emails({list(self)})" + + def __str__(self): + return "\n".join(self)