From 9be87a320c44a245602c6e537be02691075d681d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Recep=20G=C3=B6ktu=C4=9F=20Avc=C4=B1?= <220316021@ogr.cbu.edu.tr> Date: Fri, 6 Nov 2026 18:04:24 +0300 Subject: [PATCH] Create Emails class for email validation and storage Implement an Emails class to manage a list of unique, validated email addresses. --- Week05/emails_recep_goktug_avci.py | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Week05/emails_recep_goktug_avci.py diff --git a/Week05/emails_recep_goktug_avci.py b/Week05/emails_recep_goktug_avci.py new file mode 100644 index 00000000..3cbc5e98 --- /dev/null +++ b/Week05/emails_recep_goktug_avci.py @@ -0,0 +1,37 @@ +class Emails(list): + def __init__(self, emails=None): + if emails is None: + emails = [] + + valid_unique_emails = [] + for email in emails: + self.validate(email) + if email not in valid_unique_emails: + valid_unique_emails.append(email) + + + super().__init__(valid_unique_emails) + self.data = self + + @staticmethod + def validate(email): + if not isinstance(email, str): + raise ValueError(f"email must be a string, got {type(email)}") + if email.count("@") != 1: + raise ValueError(f"invalid email address: {email}") + + user, domain = email.split("@") + + if not user or not domain: + raise ValueError(f"invalid email address: {email}") + + if "." not in domain or domain.startswith(".") or domain.endswith("."): + raise ValueError(f"invalid email address: {email}") + + return True + + def __repr__(self): + return f"Emails({list(self)})" + + def __str__(self): + return super().__str__()