Skip to content

Commit 8ab0bb9

Browse files
authored
Merge pull request #22 from tanaya-g/main
Added class Clowder
2 parents dc946d8 + 6152d5d commit 8ab0bb9

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed

docs/source/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Cat (pyCatSim.Cat)
1616
.. autoclass:: pyCatSim.api.cat.Cat
1717
:members:
1818

19+
Clowder (pyCatSim.Clowder)
20+
""""""""""""""""""
21+
22+
.. autoclass:: pyCatSim.api.cat.Clowder
23+
:members:
24+
1925
Owner (pyCatSim.Owner)
2026
""""""""""""""""""""""
2127

pyCatSim/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
"""
44

55
from .cat import Cat
6+
from .cat import Clowder
67
from .human import Owner

pyCatSim/api/cat.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ def eat(self):
245245
return {"hunger_level": self.hunger_level, "mood": self.mood}
246246

247247

248-
249-
250248
def sleep(self, duration=0):
251249
"""
252250
Simulates the cat getting some sleep.
@@ -294,5 +292,75 @@ def sleep(self, duration=0):
294292
energy_boost = math.floor(duration/3)
295293

296294
self.energy += energy_boost
295+
296+
class Clowder:
297+
"""
298+
Represents a group of cats.
299+
300+
Parameters
301+
----------
302+
catlist: list
303+
A list of cats from the Cat class
297304
305+
Attributes
306+
----------
307+
catlist: list
308+
A list of cats from the Cat class
309+
310+
Examples
311+
--------
312+
313+
.. jupyter-execute::
314+
315+
import pyCatSim as cats
316+
group = cats.Clowder(catlist = [list_of_Cats])
317+
"""
318+
319+
def __init__(self, catlist=None):
320+
if catlist is None:
321+
catlist = []
322+
elif not all(isinstance(cat,Cat) for cat in catlist):
323+
raise TypeError("All elements of the list must be a Cat object")
324+
self.catlist = catlist
325+
326+
327+
def add_cat(self, cat):
328+
329+
"""
330+
Adds a Cat to the Clowder
331+
332+
Parameters:
333+
-----------
334+
cat: Cat
335+
the new Cat to add
336+
337+
Raises
338+
------
339+
TypeError
340+
If any of the arguments are not Cat instances.
341+
"""
342+
343+
if not isinstance(cat, Cat):
344+
raise TypeError("Only Cat objects can be added.")
345+
self.catlist.append(cat)
346+
347+
348+
def remove_cat(self,cat):
349+
"""
350+
Removes a Cat from the Clowder
298351
352+
Parameters:
353+
-----------
354+
cat: Cat
355+
the Cat to remove
356+
357+
Raises
358+
------
359+
ValueError
360+
If the Cat is not found in the clowder.
361+
"""
362+
363+
try:
364+
self.catlist.remove(cat)
365+
except ValueError:
366+
raise ValueError("Cat not found in Clowder")

pyCatSim/tests/test_api_Clowder.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Tests for Clowder Class
5+
"""
6+
7+
''' Tests for pyCatSim.api.cat.Clowder
8+
9+
Naming rules:
10+
1. class: Test{filename}{Class}{method} with appropriate camel case
11+
2. function: test_{method}_t{test_id}
12+
Notes on how to test:
13+
0. Make sure [pytest](https://docs.pytest.org) has been installed: `pip install pytest`
14+
1. execute `pytest {directory_path}` in terminal to perform all tests in all testing files inside the specified directory
15+
2. execute `pytest {file_path}` in terminal to perform all tests in the specified file
16+
3. execute `pytest {file_path}::{TestClass}::{test_method}` in terminal to perform a specific test class/method inside the specified file
17+
4. after `pip install pytest-xdist`, one may execute "pytest -n 4" to test in parallel with number of workers specified by `-n`
18+
5. for more details, see https://docs.pytest.org/en/stable/usage.html
19+
'''
20+
21+
import pytest
22+
import sys
23+
from pyCatSim import Cat,Clowder
24+
25+
class TestcatClowderInit:
26+
''' Test for Clowder instantiation '''
27+
28+
def test_init_t0(self):
29+
c = Clowder()
30+
assert c.catlist == []
31+
32+
def test_init_t1(self):
33+
cat1 = Cat(name="A")
34+
cat2 = Cat(name="B")
35+
c = Clowder([cat1, cat2])
36+
assert cat1 in c.catlist and cat2 in c.catlist
37+
38+
@pytest.mark.xfail
39+
def test_init_t2(self):
40+
Clowder(["not_a_cat"])
41+
42+
class TestcatClowderAdd:
43+
''' Test for Clowder add_cat '''
44+
45+
def test_add_cat_t0(self):
46+
c = Clowder()
47+
cat = Cat(name="Boots")
48+
c.add_cat(cat)
49+
assert cat in c.catlist
50+
51+
@pytest.mark.xfail
52+
def test_add_cat_t1(self):
53+
c = Clowder()
54+
c.add_cat("not_a_cat") # should raise TypeError
55+
56+
class TestcatClowderRemove:
57+
''' Test for Clowder remove_cat '''
58+
def test_remove_cat_t0(self):
59+
cat = Cat(name="Boots")
60+
c = Clowder([cat])
61+
c.remove_cat(cat)
62+
assert cat not in c.catlist
63+
64+
@pytest.mark.xfail
65+
def test_remove_cat_t1(self):
66+
cat1 = Cat(name="Boots")
67+
cat2 = Cat(name="Shadow")
68+
c = Clowder([cat1])
69+
c.remove_cat(cat2) # should raise ValueError

0 commit comments

Comments
 (0)