forked from Hwesta/git-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcattery.py
51 lines (37 loc) · 1.24 KB
/
cattery.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
"""Cat collection classes."""
from __future__ import absolute_import, unicode_literals, print_function
# Exceptions
class CatNotFound(Exception):
"""The requested cat was not found in the cattery."""
def __init__(self, name):
super(CatNotFound, self).__init__(
"Cat with name {!r} not found in cattery.".format(name)
)
self.name = name
# Classes
class Cattery(object):
"""A collection of cats."""
def __init__(self):
self._cats = []
@property
def num_cats(self):
return len(self._cats)
@property
def cats(self):
return self._cats
def add_cats(self, names):
"""Add cats with the specified names to the cattery.
:param names: A list of the names of cats to add to the cattery.
"""
self._cats.extend(names)
def remove_cat(self, name):
"""Remove the specified cat from the cattery.
The first cat found in the cattery with the specified name will be
removed.
:param name: The name of the cat to remove.
"""
cats = [cat for cat in self._cats if cat == name]
if len(cats) == 0:
raise CatNotFound(name)
self._cats.remove(cats[0])