From d4e93ac886b98ae5d3116ba30cf4ab137ca6abb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Tun=C3=A7?= <58087692+Batuhaanccs@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:04:53 +0300 Subject: [PATCH] Add Emails class for email validation and storage --- Week05/emails_batuhan_tunc.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Week05/emails_batuhan_tunc.py diff --git a/Week05/emails_batuhan_tunc.py b/Week05/emails_batuhan_tunc.py new file mode 100644 index 00000000..8fc24adc --- /dev/null +++ b/Week05/emails_batuhan_tunc.py @@ -0,0 +1,24 @@ +import re + + +class Emails(list): + 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): + super().__init__() + self.data = self.validate(emails) + self.extend(self.data) + + def validate(self, emails): + for email in emails: + if not isinstance(email, str): + raise ValueError("All items must be strings") + if not self.EMAIL_PATTERN.match(email): + raise ValueError(f"Invalid email address: {email}") + return list(dict.fromkeys(emails)) + + def __repr__(self): + return f"{self.__class__.__name__}({self.data!r})" + + def __str__(self): + return ", ".join(self.data)