diff --git a/Week05/emails_yunus_dermencioglu.py b/Week05/emails_yunus_dermencioglu.py new file mode 100644 index 00000000..bc11ef81 --- /dev/null +++ b/Week05/emails_yunus_dermencioglu.py @@ -0,0 +1,32 @@ +import re + + +class Emails(list): + + def __init__(self, emails): + super().__init__() + self.data = [] + + for email in emails: + self.validate(email) + + if email not in self.data: + self.data.append(email) + super().append(email) + + def validate(self, email): + if not isinstance(email, str): + raise ValueError("Email must be a string") + + pattern = r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$" + + if not re.fullmatch(pattern, email): + raise ValueError("Invalid email address") + + return True + + def __repr__(self): + return f"Emails({self.data!r})" + + def __str__(self): + return str(self.data)