From e25c368482075739b42b094323095357c6a7f08f Mon Sep 17 00:00:00 2001 From: onerozer Date: Wed, 18 Nov 2026 11:40:35 +0300 Subject: [PATCH] Create Emails class with validation for email addresses oner_ozer.py Implement Emails class to manage a list of email addresses with validation. --- Week05/emails_oner_ozer.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Week05/emails_oner_ozer.py diff --git a/Week05/emails_oner_ozer.py b/Week05/emails_oner_ozer.py new file mode 100644 index 00000000..d9a4acf0 --- /dev/null +++ b/Week05/emails_oner_ozer.py @@ -0,0 +1,29 @@ +class Emails(list): + + def __init__(self, initial_emails=None): + super().__init__() + self.data = self + if initial_emails is not None: + self.validate(initial_emails) + + def validate(self, emails_to_check): + + for item in emails_to_check: + if not isinstance(item, str): + raise ValueError("List can only contain strings") + + if "@" not in item: + raise ValueError("Invalid email format") + + domain_part = item.split("@")[1] + if "." not in domain_part: + raise ValueError("Invalid email format") + + if item not in self: + self.append(item) + + def __repr__(self): + return f"Emails({super().__repr__()})" + + def __str__(self): + return super().__str__()