Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Week05/emails_darya_leven.py
Original file line number Diff line number Diff line change
@@ -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)
Loading