Skip to content

Commit e199679

Browse files
authored
Merge branch 'main' into bathe-feature
2 parents 95c97f7 + a53fb85 commit e199679

File tree

11 files changed

+88
-1
lines changed

11 files changed

+88
-1
lines changed

pyCatSim/api/cat.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77

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

12+
import random
1113
import difflib
1214
import math
1315

@@ -237,6 +239,25 @@ def bathe(self):
237239
self.mood -= 1
238240
self.health += 1
239241

242+
243+
def show(self):
244+
"""
245+
246+
247+
Returns
248+
-------
249+
None.
250+
251+
"""
252+
253+
possible_colors = ['tabby', 'black', 'orange', 'tortoiseshell', 'tuxedo']
254+
255+
try:
256+
display.show(self.color)
257+
except:
258+
color = random.choice(possible_colors)
259+
display.show(color)
260+
240261

241262
def groom(self):
242263

pyCatSim/images/black.jpg

1.42 MB
Loading

pyCatSim/images/orange.jpg

2.45 MB
Loading

pyCatSim/images/tabby.jpg

2.58 MB
Loading

pyCatSim/images/tortoiseshell.jpg

6.06 MB
Loading

pyCatSim/images/tuxedo.jpg

2.85 MB
Loading

pyCatSim/tests/test_api_Cat.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import pytest
2222
import sys
2323
from pyCatSim import Cat
24+
from PIL import Image
2425

2526
class TestcatCatInit:
2627
''' Test for Cat instantiation '''
@@ -99,6 +100,29 @@ def test_bathe_t0(self):
99100
cat.bathe()
100101
assert cat.mood == 1
101102
assert cat.health == 5
103+
104+
105+
class TestcatCatShow:
106+
''' Tests for the Cat.show() method '''
107+
108+
def test_show_no_color_returns_image(self):
109+
""" test that show() returns an image when color is None (random) """
110+
cat = Cat(name="Boots", color=None)
111+
img = cat.show()
112+
assert isinstance(img, Image.Image) # PIL image check
113+
114+
@pytest.mark.parametrize("color", ['tabby', 'black', 'orange', 'tortoiseshell', 'tuxedo']) # Example colors
115+
def test_show_with_color_returns_image(self, color):
116+
""" test that show() returns an image for specified color """
117+
cat = Cat(name="Boots", color=color)
118+
img = cat.show()
119+
assert isinstance(img, Image.Image)
120+
121+
def test_show_invalid_color_raises(self):
122+
""" test that show() raises ValueError for invalid color """
123+
cat = Cat(name="Boots", color="invalid_color")
124+
with pytest.raises(ValueError):
125+
cat.show()
102126

103127
class TestcatCatGroom:
104128
''' Test for the groom function'''

pyCatSim/utils/__init__.py

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

77
from .noises import *
8+
from .display import *
89
from .facts import *

pyCatSim/utils/display.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
import random
3+
from pathlib import Path
4+
import matplotlib.pyplot as plt
5+
import matplotlib.image as mpimg
6+
7+
__all__=['show']
8+
9+
# Path to the sound files
10+
IMG_DIR = Path(__file__).parents[1].joinpath("images").resolve()
11+
12+
13+
def show(color):
14+
"""
15+
16+
17+
Parameters
18+
----------
19+
color : TYPE
20+
DESCRIPTION.
21+
22+
Returns
23+
-------
24+
None.
25+
26+
"""
27+
28+
29+
30+
filename = f"{color}.jpg"
31+
image_path = os.path.join(IMG_DIR, filename)
32+
33+
34+
img = mpimg.imread(image_path)
35+
plt.imshow(img)
36+
plt.axis('off')
37+
plt.show()

pyCatSim/utils/noises.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from playsound import playsound
1313
import os
1414
from pathlib import Path
15+
import random
16+
import matplotlib.pyplot as plt
17+
import matplotlib.image as mpimg
1518

1619
# Path to the sound files
1720
SOUND_DIR = Path(__file__).parents[1].joinpath("sounds").resolve()

0 commit comments

Comments
 (0)