From 4523407e6f389cf134643f71f0d838761816de6d Mon Sep 17 00:00:00 2001 From: Darya Leven Date: Fri, 20 Nov 2026 20:09:07 +0300 Subject: [PATCH] Add Emails class for email validation and storage Created a class to validate and store unique email addresses. --- Week05/emails_darya_leven.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Week05/emails_darya_leven.py diff --git a/Week05/emails_darya_leven.py b/Week05/emails_darya_leven.py new file mode 100644 index 00000000..3758ae5d --- /dev/null +++ b/Week05/emails_darya_leven.py @@ -0,0 +1,35 @@ +import re + + +# Class to store unique and valid email addresses +class Emails(list): + def __init__(self, emails): + # Validate emails and save them + valid = self.validate(emails) + super().__init__(valid) + self.data = valid + + def validate(self, emails): + clean_list = [] + pattern = r"^[\w.+-]+@[\w-]+\.[\w.-]+$" + + for item in emails: + # Check if item is a string + if not isinstance(item, str): + raise ValueError("Email must be a string") + # Check if email format is valid + if not re.match(pattern, item): + raise ValueError("Invalid email address") + # Add only unique emails + if item not in clean_list: + clean_list.append(item) + + return clean_list + + def __repr__(self): + # Return code representation of the object + return f"Emails({list(self)})" + + def __str__(self): + # Return readable string of emails + return "\n".join(self)