From 2cfc1afb23602e629c7b64342c2aefd26a7e1004 Mon Sep 17 00:00:00 2001 From: Beyza Arman Date: Mon, 2 Nov 2026 21:31:27 +0300 Subject: [PATCH] Add Week05 Emails class --- Week05/emails_beyza_arman.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Week05/emails_beyza_arman.py diff --git a/Week05/emails_beyza_arman.py b/Week05/emails_beyza_arman.py new file mode 100644 index 00000000..93e42384 --- /dev/null +++ b/Week05/emails_beyza_arman.py @@ -0,0 +1,31 @@ +import re + + +class Emails(list): + """A list of unique, validated email addresses.""" + + def __init__(self, emails): + unique = [] + for email in emails: + self.validate(email) + if email not in unique: + unique.append(email) + super().__init__(unique) + + @property + def data(self): + return self + + @staticmethod + def validate(email): + if not isinstance(email, str): + raise ValueError("Email addresses must be strings") + if re.fullmatch(r"[^\s@]+@[^\s@.]+(?:\.[^\s@.]+)+", email) is None: + raise ValueError("Invalid email address") + return True + + def __repr__(self): + return f"Emails({list(self)!r})" + + def __str__(self): + return ", ".join(self)