-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress_book.py
31 lines (25 loc) · 1 KB
/
address_book.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from collections import UserDict
class RecordNotFoundError(KeyError):
"""Exception raised when a record is not found."""
pass
class RecordAlreadyExistsError(KeyError):
"""Exception raised when a record already exists."""
pass
class AddressBook(UserDict):
"""A simple address address book implementation."""
def add_record(self, record):
"""Add a record to the address book."""
if record.name.value in self.data:
raise RecordAlreadyExistsError(f"Record with name '{record.name.value}' already exists.")
self.data[record.name.value] = record
def find(self, name):
"""Find a record by name."""
try:
return self.data[name]
except KeyError:
raise RecordNotFoundError(f"Record with name '{name}' not found.")
def delete(self, name):
"""Delete a record by name."""
if name not in self.data:
raise RecordNotFoundError(f"Record with name '{name}' not found.")
del self.data[name]