Created
April 10, 2021 15:15
-
-
Save RosanaRufer/d8c0599999ca4e603c8c5168e16f8486 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Email: | |
@classmethod | |
def from_text(cls, address): | |
if '@' not in address: | |
raise ValueError("Eamil address must contain '@'") | |
local_part, _, domain_part = address.partition('@') | |
return cls(local_part, domain_part) | |
def __init__(self, local_part, domain_part): | |
if len(local_part) + len(domain_part) > 255: | |
raise ValueError("Email address too long") | |
self._parts = (local_part, domain_part) | |
def __str__(self): | |
return '@'.join(self._parts) | |
def __repr__(self): | |
return "Email(local_part{!r}, domain_part={!r})".format(*self._parts) | |
def __eq__(self, rhs): | |
if not isinstance(rhs, Email): | |
raise NotImplementedError | |
return self._parts == rhs._parts | |
def __ne__(self, rhs): | |
return not (self == rhs) | |
def __hash__(self): | |
return hash(self._parts) | |
@property | |
def local(self): | |
return self._parts[0] | |
@property | |
def domain(self): | |
return self._parts[1] | |
def replace(self, local=None, domain=None): | |
return Email(local_part=local or self._parts[0], domain_part=domain or self._parts[1]) | |
rosana = Email('rosana', 'tails.com') | |
paco = Email('paco', 'tails.com') | |
rosana.__eq__(paco) | |
rosana == paco |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment