Skip to content

Commit 559458a

Browse files
authored
Merge branch 'main' into main
2 parents fc94d36 + b314bd3 commit 559458a

9 files changed

Lines changed: 238 additions & 6 deletions

File tree

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2-
3-
__pycache__
4-
./docs/_build/**
2+
/pyCatSim/__pycache__
3+
/pyCatSim/api/__pycache__
4+
**/__pycache__
5+
./docs/_build/**

pyCatSim/api/cat.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
from ..utils import noises
9+
from ..utils import facts
910

1011
import difflib
1112
import math
@@ -61,6 +62,24 @@ class Cat:
6162
nutmeg = cats.Cat(name='Nutmeg', age = 3, color = 'tortoiseshell')
6263
6364
"""
65+
66+
def give_fact(self):
67+
"""
68+
calls ..utils.random_facts() and return a random fact about cats
69+
70+
Returns
71+
-------
72+
str
73+
A fact randomly chosen from a pre-defined fact pool
74+
--------
75+
76+
.. jupyter-execute::
77+
78+
import pyCatSim as cats
79+
nutmeg = cats.Cat(name='Nutmeg', age = 3, color = 'tortoiseshell')
80+
nutmeg.give_fact()
81+
"""
82+
return facts.random_facts()
6483

6584
def __init__(self, name, age=None, color=None, mood=0, hunger_level=0,
6685
energy=0, health=0):
@@ -241,3 +260,4 @@ def sleep(self, duration=0):
241260

242261
self.energy += energy_boost
243262

263+

pyCatSim/api/human.py

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
"""
66

77
from ..api.cat import Cat
8+
from ..utils import facts
89

910
class Owner:
1011
"""
11-
Represents a cat owner who can care for one or more cats.
12+
Represents a cat owner who can care for one or more cats
1213
1314
Parameters
1415
----------
@@ -48,6 +49,27 @@ class Owner:
4849
print([cat.name for cat in owner2.cats_owned])
4950
5051
"""
52+
53+
def give_fact(self):
54+
"""
55+
calls ..utils.random_facts() and return a random fact about cats
56+
57+
Returns
58+
-------
59+
str
60+
A fact randomly chosen from a pre-defined fact pool
61+
62+
Examples
63+
--------
64+
65+
.. jupyter-execute::
66+
67+
import pyCatSim as cats
68+
nutmeg = cats.Cat(name='Nutmeg', age = 3, color = 'tortoiseshell')
69+
nutmeg.give_fact()
70+
"""
71+
return facts.random_facts()
72+
5173
def __init__(self, name, cats_owned):
5274

5375
if isinstance(cats_owned, Cat):
@@ -60,3 +82,84 @@ def __init__(self, name, cats_owned):
6082

6183
self.name = name
6284
self.cats_owned = cats_owned
85+
86+
def adopt(self, cats_object):
87+
88+
"""
89+
Add a Cat object or a list of Cat objects to an owner's cats.
90+
91+
Parameters
92+
----------
93+
cats_object: the cat to be added to the list
94+
95+
Raises
96+
------
97+
TypeError
98+
If any of the arguments are not Cat.
99+
ValueError
100+
If cats_object is not Cat.
101+
102+
Examples
103+
--------
104+
105+
.. jupyter-execute::
106+
107+
import pyCatSim as cats
108+
109+
cat1 = cats.Cat(name="Whiskers")
110+
cat2 = cats.Cat(name="Boots", color="tabby")
111+
owner1 = Owner(name="Sasha", cats_owned=cat1)
112+
owner2 = Owner(name="Liam", cats_owned=[cat1, cat2])
113+
114+
chestnut = cats.Cat(name='Chestnut', age = 4, color = 'tabby')
115+
nutmeg = cats.Cat(name='Nutmeg', age = 3, color = 'tortoiseshell')
116+
owner1.adopt(owner1,nutmeg)
117+
owner2.adopt(owner2,[chestnug,nutmeg])
118+
119+
print(owner1.name)
120+
print([cat.name for cat in owner2.cats_owned])
121+
122+
123+
"""
124+
if isinstance(cats_object, Cat):
125+
self.cats_owned.append(cats_object)
126+
elif isinstance(cats_object, list):
127+
if not all(isinstance(cat, Cat) for cat in cats_object):
128+
raise TypeError("All elements in cats_object must be instances of Cat.")
129+
else:
130+
self.cats_owned+=cats_object
131+
else:
132+
raise TypeError("cats_owned must be a Cat instance or a list of Cat instances.")
133+
134+
135+
def groom(self,Cat):
136+
"""
137+
Simulates an owner grooming one cat, increasing its mood by one
138+
139+
Parameters
140+
----------
141+
Cat : pyCatSim.Cat
142+
a pyCatSim.Cat object that you would like to groom.
143+
144+
Returns
145+
-------
146+
None.
147+
148+
149+
Examples
150+
--------
151+
.. jupyter-execute::
152+
153+
from pyCatSim import Cat, Owner
154+
155+
cat1 = Cat(name="Whiskers")
156+
157+
Deborah = Owner(name="Deborah", cats_owned=cat1)
158+
159+
Deborah.groom(cat1)
160+
161+
"""
162+
163+
Cat.mood += 1
164+
165+

pyCatSim/tests/test_api_Cat.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,27 @@ def test_sleep_t0(self, duration):
111111

112112
if duration < 3:
113113
assert cat.energy == 0
114+
115+
116+
class TestcatCatFact:
117+
''' Test for the give_fact function'''
118+
119+
def test_give_fact_t0(self):
120+
cat=Cat(name="Boots", age=2, color="tabby", mood=2, hunger_level=-1,
121+
energy = 2, health = 3)
122+
123+
cat.give_fact()
124+
125+
assert cat.give_fact() in [
126+
"Cats sleep for 70% of their lives.",
127+
"A group of cats is called a clowder.",
128+
"Cats can rotate their ears 180 degrees.",
129+
"The world's oldest cat lived to be 38 years old.",
130+
"Cats have five toes on their front paws, but only four on the back.",
131+
"A cat can jump up to six times its length.",
132+
"Each cat's nose print is unique, like a human fingerprint.",
133+
"Cats use their whiskers to detect changes in their surroundings.",
134+
"The average house cat can run at speeds up to 30 mph.",
135+
"Cats meow only to communicate with humans."
136+
]
137+

pyCatSim/tests/test_api_Owner.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,47 @@ def test_init_t1(self):
4040

4141
assert owner1.name == 'Liam'
4242
assert type(owner1.cats_owned) is list
43-
assert len(owner1.cats_owned) == 2
43+
assert len(owner1.cats_owned) == 2
44+
45+
46+
class TesthumanOwnerFact:
47+
''' Test for the give_fact function'''
48+
49+
def test_give_fact_t0(self):
50+
cat1 = Cat(name="Whiskers")
51+
owner1 = Owner(name="Sasha", cats_owned=cat1)
52+
53+
owner1.give_fact()
54+
55+
56+
class TesthumanOwnerAdopt:
57+
def test_adopt_t0(self):
58+
59+
cat1 = Cat(name="Whiskers")
60+
cat2 = Cat(name="Boots", color="tabby")
61+
owner1 = Owner(name="Sasha", cats_owned=cat1)
62+
owner2 = Owner(name="Liam", cats_owned=[cat1, cat2])
63+
chestnut = Cat(name='Chestnut', age = 4, color = 'tabby')
64+
nutmeg = Cat(name='Nutmeg', age = 3, color = 'tortoiseshell')
65+
66+
new_cat=chestnut
67+
owner1.adopt(new_cat)
68+
assert owner1.cats_owned[-1] == new_cat
69+
70+
new_cat=[chestnut,nutmeg]
71+
owner2.adopt(new_cat)
72+
assert owner2.cats_owned[-len(new_cat):]==new_cat
73+
74+
class TesthumanActions:
75+
''' Test for Owner action success '''
76+
77+
def test_groom_t0(self):
78+
cat1 = Cat(name="Whiskers",mood=7)
79+
owner1 = Owner(name="Sasha", cats_owned=cat1)
80+
81+
owner1.groom(cat1)
82+
83+
assert cat1.mood == 8
84+
85+
86+

pyCatSim/utils/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
Contains all basic block functions for pyCatSim.
55
"""
66

7-
from .noises import *
7+
from .noises import *
8+
from .facts import *
-273 Bytes
Binary file not shown.
-2.08 KB
Binary file not shown.

pyCatSim/utils/facts.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
# __all__=['random_facts']
3+
4+
import random
5+
6+
def random_facts():
7+
"""
8+
calls up random facts about cats
9+
10+
Returns
11+
-------
12+
str
13+
A fact randomly chosen from a pre-defined fact pool
14+
15+
Examples
16+
--------
17+
18+
.. jupyter-execute::
19+
20+
import pyCatSim as cats
21+
nutmeg = cats.Cat(name='Nutmeg', age = 3, color = 'tortoiseshell')
22+
nutmeg.give_fact()
23+
24+
"""
25+
26+
cat_facts = [
27+
"Cats sleep for 70% of their lives.",
28+
"A group of cats is called a clowder.",
29+
"Cats can rotate their ears 180 degrees.",
30+
"The world's oldest cat lived to be 38 years old.",
31+
"Cats have five toes on their front paws, but only four on the back.",
32+
"A cat can jump up to six times its length.",
33+
"Each cat's nose print is unique, like a human fingerprint.",
34+
"Cats use their whiskers to detect changes in their surroundings.",
35+
"The average house cat can run at speeds up to 30 mph.",
36+
"Cats meow only to communicate with humans."
37+
]
38+
return random.choice(cat_facts)
39+
40+

0 commit comments

Comments
 (0)