File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ import re
2+
3+ class Emails (list ):
4+ """
5+ Emails class that extends list and validates email addresses.
6+ """
7+ def __init__ (self , email_list ):
8+ validated_data = self .validate (email_list )
9+ super ().__init__ (validated_data )
10+ self .data = list (validated_data )
11+
12+ @staticmethod
13+ def validate (email_list ):
14+ """
15+ Validates that all items are strings and follow email format.
16+ Removes duplicates while preserving order (or just unique set).
17+ """
18+ seen = []
19+ email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
20+
21+ for email in email_list :
22+ if not isinstance (email , str ):
23+ raise ValueError ("All items must be strings." )
24+
25+ if not re .match (email_regex , email ):
26+ raise ValueError (f"Invalid email format: { email } " )
27+
28+ if email not in seen :
29+ seen .append (email )
30+
31+ return seen
32+
33+ def __repr__ (self ):
34+ """
35+ Returns a string representation that can recreate the object.
36+ """
37+ return f"Emails({ list (self )} )"
38+
39+ def __str__ (self ):
40+ """
41+ Returns a user-friendly string representation.
42+ """
43+ return f"Email List: { list (self )} "
You can’t perform that action at this time.
0 commit comments