Skip to content

Commit 0fa96d1

Browse files
authored
Merge pull request #30 from kurtlindberg/main
Add sleep method to Cat class
2 parents b314bd3 + 559458a commit 0fa96d1

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

pyCatSim/api/cat.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ..utils import facts
1010

1111
import difflib
12+
import math
1213

1314
class Cat:
1415

@@ -210,4 +211,53 @@ def play(self, mood_boost=1, hunger_boost=1, energy_boost=-1):
210211
self.mood += mood_boost
211212
self.hunger_level += hunger_boost
212213
self.energy += energy_boost
214+
215+
def sleep(self, duration=0):
216+
"""
217+
Simulates the cat getting some sleep.
218+
219+
Sleep() causes the cat to sleep for an optionally-specified duration (hrs; default=0).
220+
For every 3 hours the cat sleeps, its energy level increases increases by 1 (rounded down
221+
to the nearest integer). For example, having the cat sleeping for a duration of 5 hours raises
222+
its energy level by 1.
223+
224+
Parameters
225+
----------
226+
duration : int or float, optional
227+
Number of hours the cat sleeps. Must be an integer or float. The default is 0.
228+
229+
Raises
230+
------
231+
TypeError
232+
If duration is neither an integer nor float.
233+
ValueError
234+
If duration is not positive or is greater than 16.
235+
236+
Examples
237+
--------
238+
239+
..jupyter-execute::
240+
241+
import pyCatSim as cats
242+
nutmeg = cats.Cat(name='Nutmeg', age = 3, color = 'tortoiseshell')
243+
nutmeg.sleep(duration=5)
244+
245+
"""
246+
247+
# Enforce duration type is int or float
248+
if type(duration) != int:
249+
if type(duration) != float:
250+
raise TypeError("duration must be an integer or float")
251+
252+
# Enforce min (0 hrs) and max duration (16 hrs)
253+
if duration < 0:
254+
raise ValueError("Cats cannot sleep for negative hours. User-specified duration must be positive")
255+
if duration > 16:
256+
raise ValueError("Cats should not sleep for more than 16 hours. User-specified duration must be less than 16")
257+
258+
# Cat gains 1 energy level for every 3 hours of sleep (rounded-down; floor())
259+
energy_boost = math.floor(duration/3)
260+
261+
self.energy += energy_boost
262+
213263

pyCatSim/tests/test_api_Cat.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ def test_play_t0(self):
9191
assert cat.hunger_level == 0
9292
assert cat.energy == 1
9393

94+
class TestcatCatSleep:
95+
''' Test for the sleep function '''
96+
97+
@pytest.mark.parametrize(('duration'),
98+
[
99+
(0),
100+
(1),
101+
(4.4),
102+
pytest.param("kitty", marks=pytest.mark.xfail),
103+
pytest.param(-1, marks=pytest.mark.xfail),
104+
pytest.param(17, marks=pytest.mark.xfail)
105+
]
106+
)
107+
def test_sleep_t0(self, duration):
108+
cat = Cat(name="Boots", color="tabby")
109+
110+
cat.sleep(duration)
111+
112+
if duration < 3:
113+
assert cat.energy == 0
114+
94115

95116
class TestcatCatFact:
96117
''' Test for the give_fact function'''
@@ -113,7 +134,4 @@ def test_give_fact_t0(self):
113134
"The average house cat can run at speeds up to 30 mph.",
114135
"Cats meow only to communicate with humans."
115136
]
116-
117-
118-
119-
137+

0 commit comments

Comments
 (0)