Skip to content

Commit 4f421cd

Browse files
authored
Merge pull request #21 from ballight42/main
Add adopt function
2 parents 66d3c70 + 468f33e commit 4f421cd

2 files changed

Lines changed: 70 additions & 6 deletions

File tree

pyCatSim/api/human.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class Owner:
1010
"""
11-
Represents a cat owner who can care for one or more cats.
11+
Represents a cat owner who can care for one or more cats
1212
1313
Parameters
1414
----------
@@ -60,6 +60,55 @@ def __init__(self, name, cats_owned):
6060

6161
self.name = name
6262
self.cats_owned = cats_owned
63+
64+
def adopt(self, cats_object):
65+
66+
"""
67+
Add a Cat object or a list of Cat objects to an owner's cats.
68+
69+
Parameters
70+
----------
71+
cats_object: the cat to be added to the list
72+
73+
Raises
74+
------
75+
TypeError
76+
If any of the arguments are not Cat.
77+
ValueError
78+
If cats_object is not Cat.
79+
80+
Examples
81+
--------
82+
83+
.. jupyter-execute::
84+
85+
import pyCatSim as cats
86+
87+
cat1 = cats.Cat(name="Whiskers")
88+
cat2 = cats.Cat(name="Boots", color="tabby")
89+
owner1 = Owner(name="Sasha", cats_owned=cat1)
90+
owner2 = Owner(name="Liam", cats_owned=[cat1, cat2])
91+
92+
chestnut = cats.Cat(name='Chestnut', age = 4, color = 'tabby')
93+
nutmeg = cats.Cat(name='Nutmeg', age = 3, color = 'tortoiseshell')
94+
owner1.adopt(owner1,nutmeg)
95+
owner2.adopt(owner2,[chestnug,nutmeg])
96+
97+
print(owner1.name)
98+
print([cat.name for cat in owner2.cats_owned])
99+
100+
101+
"""
102+
if isinstance(cats_object, Cat):
103+
self.cats_owned.append(cats_object)
104+
elif isinstance(cats_object, list):
105+
if not all(isinstance(cat, Cat) for cat in cats_object):
106+
raise TypeError("All elements in cats_object must be instances of Cat.")
107+
else:
108+
self.cats_owned+=cats_object
109+
else:
110+
raise TypeError("cats_owned must be a Cat instance or a list of Cat instances.")
111+
63112

64113
def groom(self,Cat):
65114
"""

pyCatSim/tests/test_api_Owner.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ def test_init_t1(self):
4141
assert owner1.name == 'Liam'
4242
assert type(owner1.cats_owned) is list
4343
assert len(owner1.cats_owned) == 2
44+
45+
46+
class TesthumanOwnerAdopt:
47+
def test_adopt_t0(self):
48+
49+
cat1 = Cat(name="Whiskers")
50+
cat2 = Cat(name="Boots", color="tabby")
51+
owner1 = Owner(name="Sasha", cats_owned=cat1)
52+
owner2 = Owner(name="Liam", cats_owned=[cat1, cat2])
53+
chestnut = Cat(name='Chestnut', age = 4, color = 'tabby')
54+
nutmeg = Cat(name='Nutmeg', age = 3, color = 'tortoiseshell')
55+
56+
new_cat=chestnut
57+
owner1.adopt(new_cat)
58+
assert owner1.cats_owned[-1] == new_cat
59+
60+
new_cat=[chestnut,nutmeg]
61+
owner2.adopt(new_cat)
62+
assert owner2.cats_owned[-len(new_cat):]==new_cat
4463

4564
class TesthumanActions:
4665
''' Test for Owner action success '''
@@ -54,8 +73,4 @@ def test_groom_t0(self):
5473
assert cat1.mood == 8
5574

5675

57-
58-
59-
60-
61-
76+

0 commit comments

Comments
 (0)